Picturebox mit autom. Grössenanpassung des Bilds

    • VB.NET
    • .NET (FX) 4.5–4.8

    Es gibt 4 Antworten in diesem Thema. Der letzte Beitrag () ist von VaporiZed.

      Picturebox mit autom. Grössenanpassung des Bilds

      Hi Ihr,

      nachdem ich einige Zeit damit verbracht habe, Bilder in meiner Anwendung stets passend in einem Parent-Container und mit möglicher Auswahl vom PicSizeMode Normal, Center, Zoom und Stretch anzeigen zu können, hier mein Script:
      Vielleicht kann es ja mal jemand brauchen.

      Das Bild resized automatisch beim Resizen der Mainform, wenn das Tsc_PicEditor an dieser verankert ist.

      Ich bitte um Nachsicht, wenn es nicht perfekt gecodet ist, bin aber noch nicht so lange mit VB.Net unterwegs.

      Benötigt werden:
      - Ein ToolStripControl als Container für die PicBox mit Namen "Tsc_PicEditor"
      - Eine PictureBox im CenterPanel des o.g. Tsc_PicEditor mit Namen "PictureBox"
      - Fünf Buttons für MainformPicSizeMode Normal, Center, Zoom und Stretch sowie Bild Beschneiden
      - Vier Buttons für Links und Rechts drehen, sowie Horizontal und Vertikal spiegeln

      VB.NET-Quellcode

      1. Dim MainformPicSizeMode as integer = 0
      2. ' Rubberband
      3. Dim inRubberBand As Boolean = False
      4. dim clearImage as Boolean = false
      5. Dim cropX As Integer
      6. Dim cropY As Integer
      7. Dim cropWidth As Integer
      8. Dim cropHeight As Integer
      9. Dim mousePosX As Integer
      10. Dim mousePosY As Integer
      11. Dim mouseMovePosX As Integer
      12. Dim mouseMovePosY As Integer
      13. Dim oCropX As Integer
      14. Dim oCropY As Integer
      15. Dim cropBitmap As Bitmap
      16. Public cropPen As Pen
      17. Public cropPenSize As Integer = 2
      18. Public cropDashStyle As Drawing2D.DashStyle = Drawing2D.DashStyle.Solid
      19. Public cropPenColor As Color = Color.Red
      20. Private Sub StateChangedForm_LocationChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.LocationChanged
      21. FormWindowState_Changed(False)
      22. End Sub
      23. Private Sub StateChangedForm_SizeChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.SizeChanged
      24. FormWindowState_Changed(True)
      25. End Sub
      26. Private Sub FormWindowState_Changed(grössenänderung As Boolean)
      27. Select Case Me.WindowState
      28. Case = FormWindowState.Normal
      29. If grössenänderung = True andalso tsc_PicEditor.Visible = True Then
      30. resize_PicEditor()
      31. End If
      32. Case = FormWindowState.Maximized
      33. If grössenänderung = True andalso tsc_PicEditor.Visible = True Then
      34. tsc_PicEditor_PictureBox.Visible = False
      35. resize_PicEditor()
      36. End If
      37. End Select
      38. End Sub
      39. Private Sub Tsc_PicEditor_PictureBox_Paint(sender As Object, e As PaintEventArgs) Handles tsc_PicEditor_PictureBox.Paint
      40. If clearImage Then
      41. clearImage = False
      42. e.Graphics.Clear(Color.White)
      43. End If
      44. End Sub
      45. Private Sub Tsc_PicEditor_PictureBox_MouseEnter(sender As Object, e As EventArgs) Handles tsc_PicEditor_PictureBox.MouseEnter
      46. tsc_PicEditor.Cursor = Cursors.Cross
      47. End Sub
      48. Private Sub Tsc_PicEditor_PictureBox_MouseLeave(sender As Object, e As EventArgs) Handles tsc_PicEditor_PictureBox.MouseLeave
      49. tsc_PicEditor.Cursor = Cursors.Default
      50. End Sub
      51. Private Sub Tsc_PicEditor_PictureBox_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles tsc_PicEditor_PictureBox.MouseDown
      52. Try
      53. cropPenColor = My.Settings.PicEditor_RubberbandFarbe
      54. cropPenSize = My.Settings.PicEditor_RubberbandSize
      55. cropWidth = 0
      56. cropHeight = 0
      57. inRubberBand = False
      58. If e.Button = Windows.Forms.MouseButtons.Left Then
      59. cropX = e.X
      60. cropY = e.Y
      61. cropPen = New Pen(cropPenColor, cropPenSize) With {
      62. .DashStyle = DashStyle.DashDotDot
      63. }
      64. End If
      65. tsc_PicEditor_PictureBox.Refresh()
      66. Catch exc As System.Exception
      67. End Try
      68. End Sub
      69. Private Sub Tsc_PicEditor_PictureBox_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles tsc_PicEditor_PictureBox.MouseMove
      70. Try
      71. If tsc_PicEditor_PictureBox.Image Is Nothing Then Exit Sub
      72. mousePosX = e.X
      73. mousePosY = e.Y
      74. If e.Button = Windows.Forms.MouseButtons.Left Then
      75. tsc_PicEditor_PictureBox.Refresh()
      76. If e.X <= tsc_PicEditor_PictureBox.Width - My.Settings.PicEditor_RubberbandSize Then
      77. cropWidth = e.X - cropX
      78. mouseMovePosX = e.X
      79. Else
      80. mouseMovePosX = tsc_PicEditor_PictureBox.Width
      81. cropWidth = tsc_PicEditor_PictureBox.Width - cropX - My.Settings.PicEditor_RubberbandSize
      82. End If
      83. If e.Y <= tsc_PicEditor_PictureBox.Height - My.Settings.PicEditor_RubberbandSize Then
      84. cropHeight = e.Y - cropY
      85. mouseMovePosY = e.Y
      86. Else
      87. mouseMovePosY = tsc_PicEditor_PictureBox.Height
      88. cropHeight = tsc_PicEditor_PictureBox.Height - cropY - My.Settings.PicEditor_RubberbandSize
      89. End If
      90. inRubberBand = True
      91. tsc_PicEditor_PictureBox.CreateGraphics.DrawRectangle(cropPen, cropX, cropY, cropWidth, cropHeight)
      92. End If
      93. System.Windows.Forms.Application.DoEvents()
      94. Catch exc As System.Exception
      95. If Err.Number = 5 Then Exit Sub
      96. End Try
      97. End Sub
      98. Private Sub Tsc_PicEditor_PictureBox_MouseUp(sender As Object, e As MouseEventArgs) Handles tsc_PicEditor_PictureBox.MouseUp
      99. inRubberBand = False
      100. End Sub
      101. Private Sub Bild_LadenUndAnzeigen(name As String)
      102. tsc_PicEditor_PictureBox.Visible = False
      103. Using fs As New System.IO.FileStream(name, IO.FileMode.Open)
      104. tsc_PicEditor_PictureBox.Image = New Bitmap(Image.FromStream(fs))
      105. End Using
      106. resize_PicEditor()
      107. inRubberBand = False
      108. End Sub
      109. Private Sub Bild_Normal()
      110. tsc_PicEditor_PictureBox.Visible = False
      111. MainformPicSizeMode = 0
      112. resize_PicEditor()
      113. End Sub
      114. Private Sub Bild_Center()
      115. tsc_PicEditor_PictureBox.Visible = False
      116. MainformPicSizeMode = 1
      117. resize_PicEditor()
      118. End Sub
      119. Private Sub Bild_Zoom()
      120. tsc_PicEditor_PictureBox.Visible = False
      121. MainformPicSizeMode = 2
      122. resize_PicEditor()
      123. End Sub
      124. Private Sub Bild_Stretch()
      125. tsc_PicEditor_PictureBox.Visible = False
      126. MainformPicSizeMode = 3
      127. resize_PicEditor()
      128. End Sub
      129. Private Sub Bild_LinksDrehen()
      130. Dim Bitmap As Image
      131. tsc_PicEditor_PictureBox.Visible = False
      132. Bitmap = tsc_PicEditor_PictureBox.Image
      133. Bitmap.RotateFlip(RotateFlipType.Rotate270FlipNone)
      134. tsc_PicEditor_PictureBox.Image = Bitmap
      135. resize_PicEditor()
      136. End Sub
      137. Private Sub Bild_RechtsDrehen()
      138. Dim Bitmap As Image
      139. tsc_PicEditor_PictureBox.Visible = False
      140. Bitmap = tsc_PicEditor_PictureBox.Image
      141. Bitmap.RotateFlip(RotateFlipType.Rotate90FlipNone)
      142. tsc_PicEditor_PictureBox.Image = Bitmap
      143. resize_PicEditor()
      144. End Sub
      145. Private Sub Bild_VertikalSpiegeln()
      146. Dim Bitmap As Image
      147. tsc_PicEditor_PictureBox.Visible = False
      148. Bitmap = tsc_PicEditor_PictureBox.Image
      149. Bitmap.RotateFlip(RotateFlipType.Rotate180FlipY)
      150. tsc_PicEditor_PictureBox.Image = Bitmap
      151. resize_PicEditor()
      152. End Sub
      153. Private Sub Bild_HorizontalSpiegeln()
      154. Dim Bitmap As Image
      155. tsc_PicEditor_PictureBox.Visible = False
      156. Bitmap = tsc_PicEditor_PictureBox.Image
      157. Bitmap.RotateFlip(RotateFlipType.Rotate180FlipX)
      158. tsc_PicEditor_PictureBox.Image = Bitmap
      159. resize_PicEditor()
      160. End Sub
      161. Private Sub Bild_Beschneiden()
      162. Try
      163. If cropWidth < 1 Then
      164. Exit Sub
      165. End If
      166. tsc_PicEditor_PictureBox.Visible = False
      167. Dim rect As New Rectangle(cropX, cropY, cropWidth, cropHeight)
      168. Dim bit As New Bitmap(tsc_PicEditor_PictureBox.Image, tsc_PicEditor_PictureBox.Width, tsc_PicEditor_PictureBox.Height)
      169. cropBitmap = New Bitmap(cropWidth, cropHeight)
      170. Dim g As Graphics = Graphics.FromImage(cropBitmap)
      171. g.InterpolationMode = Drawing2D.InterpolationMode.HighQualityBicubic
      172. g.PixelOffsetMode = Drawing2D.PixelOffsetMode.HighQuality
      173. g.CompositingQuality = Drawing2D.CompositingQuality.HighQuality
      174. g.DrawImage(bit, 0, 0, rect, GraphicsUnit.Pixel)
      175. tsc_PicEditor_PictureBox.Image = cropBitmap
      176. resize_PicEditor()
      177. Catch exc As System.Exception
      178. End Try
      179. inRubberBand = False
      180. End Sub
      181. Private Sub resize_PicEditor()
      182. Dim tmpString As String = ""
      183. tsc_PicEditor_PictureBox.SuspendLayout()
      184. If tsc_PicEditor_PictureBox.Image IsNot Nothing Then
      185. tsc_PicEditor.Width = spc_DateienInhalte.Panel2.Width
      186. tsc_PicEditor.Height = spc_DateienInhalte.Panel2.Height
      187. tsc_PicEditor_Panel.Width = tsc_PicEditor.Width - 2
      188. Dim bildWidth As Integer = tsc_PicEditor_PictureBox.Image.Width
      189. Dim bildHeight As Integer = tsc_PicEditor_PictureBox.Image.Height
      190. Dim panelWidth As Integer = tsc_PicEditor_Panel.Width
      191. Dim panelHeight As Integer = tsc_PicEditor_Panel.Height
      192. Select Case MainformPicSizeMode
      193. Case = 0 ' normal
      194. tmpString = "Originalgrösse"
      195. If ((bildWidth > panelWidth) Or (bildHeight > panelHeight)) Then
      196. tsc_PicEditor_PictureBox.Anchor = AnchorStyles.Top Or AnchorStyles.Left
      197. Else
      198. tsc_PicEditor_PictureBox.Anchor = AnchorStyles.Top Or AnchorStyles.Bottom Or AnchorStyles.Left Or AnchorStyles.Right
      199. End If
      200. tsc_PicEditor_PictureBox.SizeMode = PictureBoxSizeMode.Normal
      201. tsc_PicEditor_PictureBox.Left = 0
      202. tsc_PicEditor_PictureBox.Top = 0
      203. tsc_PicEditor_PictureBox.Width = bildWidth
      204. tsc_PicEditor_PictureBox.Height = bildHeight
      205. Case = 1 ' center
      206. tmpString = "Originalgrösse (zentriert)"
      207. tsc_PicEditor_PictureBox.Anchor = AnchorStyles.Top Or AnchorStyles.Bottom Or AnchorStyles.Left Or AnchorStyles.Right
      208. tsc_PicEditor_PictureBox.SizeMode = PictureBoxSizeMode.CenterImage
      209. If panelWidth > bildWidth Then
      210. tsc_PicEditor_PictureBox.Left = CInt((panelWidth - bildWidth) / 2)
      211. tsc_PicEditor_PictureBox.Width = bildWidth
      212. Else
      213. tsc_PicEditor_PictureBox.Left = 0
      214. tsc_PicEditor_PictureBox.Width = panelWidth
      215. End If
      216. If panelHeight > bildHeight Then
      217. tsc_PicEditor_PictureBox.Top = CInt((panelHeight - bildHeight) / 2)
      218. tsc_PicEditor_PictureBox.Height = bildHeight
      219. Else
      220. tsc_PicEditor_PictureBox.Top = 0
      221. tsc_PicEditor_PictureBox.Height = panelHeight
      222. End If
      223. Case = 2 ' Zoom
      224. tmpString = "Anzeigegrösse (Verhältnis beibehalten)"
      225. tsc_PicEditor_PictureBox.Anchor = AnchorStyles.None
      226. tsc_PicEditor_PictureBox.SizeMode = PictureBoxSizeMode.Zoom
      227. If panelWidth > 0 AndAlso panelHeight > 0 Then
      228. Dim panelVerhältnis As Single = CSng(panelWidth / panelHeight)
      229. Dim BildVerhältnis As Single = CSng(bildWidth / bildHeight)
      230. If panelVerhältnis > 1 Then
      231. ' Panel breiter als Hoch
      232. If BildVerhältnis > 1 Then
      233. ' Bild breiter als Hoch
      234. If panelVerhältnis < BildVerhältnis Then
      235. ' Bild schmaler als Panel
      236. tsc_PicEditor_PictureBox.Top = CInt((panelHeight - (panelWidth / bildWidth * bildHeight)) / 2)
      237. tsc_PicEditor_PictureBox.Left = 0
      238. tsc_PicEditor_PictureBox.Width = panelWidth - 2
      239. tsc_PicEditor_PictureBox.Height = CInt(panelWidth / bildWidth * bildHeight)
      240. Else
      241. ' Bild breiter als Panel
      242. tsc_PicEditor_PictureBox.Top = 0
      243. tsc_PicEditor_PictureBox.Left = CInt((panelWidth - (bildWidth / (bildHeight / panelHeight))) / 2)
      244. tsc_PicEditor_PictureBox.Height = panelHeight
      245. tsc_PicEditor_PictureBox.Width = CInt(bildWidth / (bildHeight / panelHeight))
      246. End If
      247. Else
      248. ' Bild höher als Breit
      249. ' Bild höher als Panel
      250. tsc_PicEditor_PictureBox.Top = 0
      251. tsc_PicEditor_PictureBox.Left = CInt((panelWidth - (panelHeight / bildHeight * bildWidth)) / 2)
      252. tsc_PicEditor_PictureBox.Height = panelHeight
      253. tsc_PicEditor_PictureBox.Width = CInt(panelHeight / bildHeight * bildWidth)
      254. End If
      255. Else
      256. ' Panel Höher als Breit
      257. If BildVerhältnis > 1 Then
      258. ' Bild breiter als hoch
      259. tsc_PicEditor_PictureBox.Top = CInt((panelHeight - (panelWidth / bildWidth * bildHeight)) / 2)
      260. tsc_PicEditor_PictureBox.Left = 0
      261. tsc_PicEditor_PictureBox.Width = panelWidth - 2
      262. tsc_PicEditor_PictureBox.Height = CInt(panelWidth / bildWidth * bildHeight)
      263. Else
      264. If panelVerhältnis < BildVerhältnis Then
      265. ' Bild höher als Panel
      266. tsc_PicEditor_PictureBox.Top = CInt((panelHeight - (panelWidth / bildWidth * bildHeight)) / 2)
      267. tsc_PicEditor_PictureBox.Left = 0
      268. tsc_PicEditor_PictureBox.Width = panelWidth - 2
      269. tsc_PicEditor_PictureBox.Height = CInt(panelWidth / bildWidth * bildHeight)
      270. Else
      271. ' Bild niedriger als Panel
      272. tsc_PicEditor_PictureBox.Top = 1
      273. tsc_PicEditor_PictureBox.Left = CInt((panelWidth - (panelHeight / bildHeight * bildWidth)) / 2)
      274. tsc_PicEditor_PictureBox.Height = panelHeight - 2
      275. tsc_PicEditor_PictureBox.Width = CInt(panelHeight / bildHeight * bildWidth)
      276. End If
      277. End If
      278. End If
      279. End If
      280. Case = 3 ' Stretch
      281. tmpString = "Anzeigegrösse (Verhältnis ignorieren)"
      282. tsc_PicEditor_PictureBox.Anchor = AnchorStyles.Top Or AnchorStyles.Bottom Or AnchorStyles.Left Or AnchorStyles.Right
      283. tsc_PicEditor_PictureBox.SizeMode = PictureBoxSizeMode.StretchImage
      284. tsc_PicEditor_PictureBox.Left = 0
      285. tsc_PicEditor_PictureBox.Top = 0
      286. tsc_PicEditor_PictureBox.Width = panelWidth - 2
      287. tsc_PicEditor_PictureBox.Height = panelHeight
      288. End Select
      289. dateiinfo = tmpString & " - (" & Format(tsc_PicEditor_PictureBox.Image.PhysicalDimension.Width, "###,###,##0") & " x " & Format(tsc_PicEditor_PictureBox.Image.PhysicalDimension.Height, "###,###,##0") & " - Format: " & Chr(34) & tsc_PicEditor_PictureBox.Image.PixelFormat.ToString.Substring(6) & Chr(34) & ")"
      290. Else
      291. dateiinfo = "Pic-Viewer - Kein Bild geladen"
      292. End If
      293. tsc_PicEditor_PictureBox.Visible = True
      294. tsc_PicEditor_PictureBox.ResumeLayout()
      295. End Sub

      Dieser Beitrag wurde bereits 3 mal editiert, zuletzt von „Dideldum“ ()

      Anstatt viele Voraussetzungen bzgl. des Codes zu machen, wäre ein Beispielprogramm sinnvoll gewesen.
      Zeile#37: If grösse = - der Parameter ist nicht definiert. Der Code sollte vor dem Posten erfolgreich getestet sein und funktionieren. Bitte nachbessern.
      Dieser Beitrag wurde bereits 5 mal editiert, zuletzt von „VaporiZed“, mal wieder aus Grammatikgründen.

      Aufgrund spontaner Selbsteintrübung sind all meine Glaskugeln beim Hersteller. Lasst mich daher bitte nicht den Spekulatiusbackmodus wechseln.

      Dieser Beitrag wurde bereits 1 mal editiert, zuletzt von „VaporiZed“ ()

      Dideldum schrieb:

      Das Bild resized automatisch beim Resizen der Mainform, wenn das Tsc_PicEditor an dieser verankert ist.
      Das kann ja auch jede PictureBox, wenn man sie mit geeignetem Docking oder Anchoring aufs Form packt.
      BeispielAnwendung wäre also wirklich nützlich, dass man sehen kann, was dieser Ansatz in der Beziehung anders macht.
      Hi Ihr
      @VaporiZed
      Danke für Eure Beiträge.
      Sorry, muss in #37 nicht grösse, sondern grössenänderung heissen

      @ErfinderDesRades
      Ich wusste nicht, dass man hier auch fertige Apps hochladen kann.
      Was muss denn in die Beispielanwendung rein?
      Gibt es dafür eine Anleitung?
      Dann bastle ich das.

      Also bei meinem Script wird die PictureBox wie in den Pics in dem ToolStripContainer angezeigt.
      Das habe ich nicht anders hinbekommen, als mit dem Code.
      Wird der TSC resized, passt sich die PicBox automatisch an.
      Wenn das auch anders und einfacher funktioniert, kann man diesen Thread gerne löschen.
      Bilder
      • Center.jpg

        161,52 kB, 1.206×1.096, 23 mal angesehen
      • Normal.jpg

        133,39 kB, 1.206×1.074, 23 mal angesehen
      • Stretch.jpg

        206,54 kB, 1.206×1.096, 25 mal angesehen
      • Zoom 1.jpg

        144,28 kB, 956×1.022, 24 mal angesehen
      • Zoom 2.jpg

        90,16 kB, 490×1.016, 25 mal angesehen
      Du kannst ein Projekt ohne bin-, obj-, .vs- und .git-Ordner und gezippt über [+ Erweiterte Antwort] hochladen. Eben ein Visual Studio Projekt, welches den Quellcode enthält. Also in dem Fall ein ganzes Testprojekt. Nur eben ohne die anfangs erwähnten Ordner.
      Dieser Beitrag wurde bereits 5 mal editiert, zuletzt von „VaporiZed“, mal wieder aus Grammatikgründen.

      Aufgrund spontaner Selbsteintrübung sind all meine Glaskugeln beim Hersteller. Lasst mich daher bitte nicht den Spekulatiusbackmodus wechseln.