Paintanwendung auf der picturebox speichern

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

Es gibt 2 Antworten in diesem Thema. Der letzte Beitrag () ist von RodFromGermany.

    Paintanwendung auf der picturebox speichern

    hallo,
    im folgenden code wird unter anderem eine paint anwendung mit dem savefildialog gespeichert.

    ich möchte das sehr gerne mit button.click durchführen, ohne dass man extra ein savefiledialog oeffnen muss.

    hat jemand einen tipp? da der code ein bisschen laender ist bitte suchfunktion savefiledialog nutzen

    vielen dank

    Quellcode

    1. Public Class PaintForm
    2. Dim g As Graphics
    3. Dim startLocation As Point
    4. Dim endLocation As Point
    5. Dim TempLocation As New Point(-1, -1)
    6. Dim TempLocation2 As New Point(-1, -1)
    7. Dim NumberOfAngle As Integer = 0
    8. Dim drawing As Boolean = False
    9. Dim CurrentColor As Color = Color.Blue
    10. Dim CurrentColor2 As Color = Color.LightSkyBlue
    11. Dim LastImage As New Bitmap(501, 501)
    12. Dim M1 As New Bitmap(501, 501)
    13. Dim M2 As New Bitmap(501, 501)
    14. Dim M3 As New Bitmap(501, 501)
    15. Dim M4 As New Bitmap(501, 501)
    16. Dim PenWidth As Single = 1.0F
    17. Dim PenPoint As Pen
    18. Dim SavedFileAddress As String = ""
    19. Dim CurrentFont As Font
    20. Function FillRegion(ByVal X As Integer, ByVal Y As Integer, ByVal FillCol As Color) As Boolean
    21. If X < 0 Or X > LastImage.Width Or Y < 0 Or Y > LastImage.Height Then
    22. Return False
    23. End If
    24. Application.DoEvents()
    25. Dim points As Stack = New Stack
    26. points.Push(New Point(X, Y))
    27. Dim Pointcolor As Color = LastImage.GetPixel(X, Y)
    28. Do
    29. Dim p As Point = CType(points.Pop(), Point)
    30. LastImage.SetPixel(p.X, p.Y, FillCol)
    31. If UpPixelHaseSameColor(p.X, p.Y, Pointcolor) Then
    32. points.Push(New Point(p.X, p.Y - 1))
    33. End If
    34. If DownPixelHaseSameColor(p.X, p.Y, Pointcolor) Then
    35. points.Push(New Point(p.X, p.Y + 1))
    36. End If
    37. If RightPixelHaseSameColor(p.X, p.Y, Pointcolor) Then
    38. points.Push(New Point(p.X + 1, p.Y))
    39. End If
    40. If LeftPixelHaseSameColor(p.X, p.Y, Pointcolor) Then
    41. points.Push(New Point(p.X - 1, p.Y))
    42. End If
    43. Loop While points.Count > 0
    44. Return True
    45. End Function
    46. Function UpPixelHaseSameColor(ByVal X As Integer, ByVal Y As Integer, ByVal Col As Color) As Boolean
    47. Dim result As Boolean = False
    48. If (Y > 0) Then
    49. If (LastImage.GetPixel(X, Y - 1) = Col) Then
    50. result = True
    51. End If
    52. End If
    53. Return result
    54. End Function
    55. Function DownPixelHaseSameColor(ByVal X As Integer, ByVal Y As Integer, ByVal Col As Color) As Boolean
    56. Dim result As Boolean = False
    57. If (Y < LastImage.Height - 1) Then
    58. If (LastImage.GetPixel(X, Y + 1) = Col) Then
    59. result = True
    60. End If
    61. End If
    62. Return result
    63. End Function
    64. Function RightPixelHaseSameColor(ByVal X As Integer, ByVal Y As Integer, ByVal Col As Color) As Boolean
    65. Dim result As Boolean = False
    66. If (X < LastImage.Width - 1) Then
    67. If (LastImage.GetPixel(X + 1, Y) = Col) Then
    68. result = True
    69. End If
    70. End If
    71. Return result
    72. End Function
    73. Function LeftPixelHaseSameColor(ByVal X As Integer, ByVal Y As Integer, ByVal Col As Color) As Boolean
    74. Dim result As Boolean = False
    75. If (X > 0) Then
    76. If (LastImage.GetPixel(X - 1, Y) = Col) Then
    77. result = True
    78. End If
    79. End If
    80. Return result
    81. End Function
    82. Sub UpdateImage()
    83. g = Graphics.FromImage(LastImage)
    84. Me.PictureBox1.Image = LastImage
    85. End Sub
    86. Sub ReloadPen(ByVal PenWd As Single, ByVal CurColor As Color)
    87. PenPoint = New Pen(CurColor, PenWd)
    88. End Sub
    89. Private Sub PictureBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown
    90. If e.Button = Windows.Forms.MouseButtons.Left Then
    91. If drawing = False Then
    92. startLocation = e.Location
    93. drawing = True
    94. If MultiAngleRadioButton.Checked Then
    95. If TempLocation.X = -1 Then
    96. TempLocation = startLocation
    97. TempLocation2 = startLocation
    98. End If
    99. endLocation = e.Location
    100. g.DrawLine(PenPoint, TempLocation, endLocation)
    101. TempLocation = endLocation
    102. ElseIf TriangleRadioButton.Checked Then
    103. If TempLocation.X = -1 Then
    104. TempLocation = startLocation
    105. TempLocation2 = startLocation
    106. End If
    107. If NumberOfAngle <= 2 Then
    108. endLocation = e.Location
    109. g.DrawLine(PenPoint, TempLocation, endLocation)
    110. TempLocation = endLocation
    111. If NumberOfAngle = 2 Then
    112. g.DrawLine(PenPoint, TempLocation, TempLocation2)
    113. TempLocation = New Point(-1, -1)
    114. NumberOfAngle = 0
    115. Else
    116. NumberOfAngle += 1
    117. End If
    118. End If
    119. End If
    120. End If
    121. ElseIf e.Button = Windows.Forms.MouseButtons.Right Then
    122. If MultiAngleRadioButton.Checked Then
    123. If TempLocation.X <> -1 Then
    124. endLocation = e.Location
    125. g.DrawLine(PenPoint, TempLocation, TempLocation2)
    126. TempLocation = New Point(-1, -1)
    127. End If
    128. End If
    129. End If
    130. End Sub
    131. Private Sub PictureBox1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseMove
    132. If drawing = True Then
    133. If LineRadioButton.Checked Then
    134. g.DrawLine(PenPoint, startLocation.X, startLocation.Y, e.X, e.Y)
    135. startLocation = e.Location
    136. UpdateImage()
    137. ElseIf EraserRadioButton.Checked Then
    138. Dim p As New Pen(Color.White, PenWidth)
    139. g.DrawLine(p, startLocation.X, startLocation.Y, e.X, e.Y)
    140. startLocation = e.Location
    141. UpdateImage()
    142. End If
    143. End If
    144. End Sub
    145. Private Sub PictureBox1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseUp
    146. If drawing Then
    147. If RectangleRadioButton.Checked Then
    148. endLocation = e.Location
    149. Dim s As Point
    150. s.X = endLocation.X - startLocation.X
    151. If s.X < 0 Then
    152. startLocation.X = endLocation.X
    153. End If
    154. s.Y = endLocation.Y - startLocation.Y
    155. If s.Y < 0 Then
    156. startLocation.Y = endLocation.Y
    157. End If
    158. s.X = Math.Abs(s.X)
    159. s.Y = Math.Abs(s.Y)
    160. g.DrawRectangle(PenPoint, New Rectangle(startLocation, s))
    161. ElseIf GradientRectAngleRadioButton.Checked Then
    162. endLocation = e.Location
    163. Dim s As Point
    164. If s.X < 0 Then
    165. startLocation.X = endLocation.X
    166. ElseIf s.X = 0 Then
    167. s.X = 1
    168. End If
    169. s.Y = endLocation.Y - startLocation.Y
    170. If s.Y < 0 Then
    171. startLocation.Y = endLocation.Y
    172. ElseIf s.Y = 0 Then
    173. s.Y = 1
    174. End If
    175. s.X = Math.Abs(s.X)
    176. s.Y = Math.Abs(s.Y)
    177. Dim b As Brush
    178. b = New Drawing2D.LinearGradientBrush(New Rectangle(startLocation, s), CurrentColor, CurrentColor2, Drawing2D.LinearGradientMode.BackwardDiagonal)
    179. g.FillRectangle(b, New Rectangle(startLocation, s))
    180. ElseIf CircleRadioButton.Checked Then
    181. endLocation = e.Location
    182. Dim s As Point
    183. s.X = endLocation.X - startLocation.X
    184. If s.X < 0 Then
    185. startLocation.X = endLocation.X
    186. End If
    187. s.Y = endLocation.Y - startLocation.Y
    188. If s.Y < 0 Then
    189. startLocation.Y = endLocation.Y
    190. End If
    191. s.X = Math.Abs(s.X)
    192. s.Y = Math.Abs(s.Y)
    193. If s.X > s.Y Then
    194. s.Y = s.X
    195. Else
    196. s.X = s.Y
    197. End If
    198. g.DrawEllipse(PenPoint, New Rectangle(startLocation, s))
    199. ElseIf ArcRadioButton.Checked Then
    200. endLocation = e.Location
    201. Dim s As Point
    202. s.X = endLocation.X - startLocation.X
    203. If s.X < 0 Then
    204. startLocation.X = endLocation.X
    205. End If
    206. s.Y = endLocation.Y - startLocation.Y
    207. If s.Y < 0 Then
    208. startLocation.Y = endLocation.Y
    209. End If
    210. s.X = Math.Abs(s.X)
    211. s.Y = Math.Abs(s.Y)
    212. If s.X > s.Y Then
    213. s.Y = s.X
    214. Else
    215. s.X = s.Y
    216. End If
    217. g.DrawArc(PenPoint, New Rectangle(startLocation, s), 0, -180)
    218. ElseIf ParallelepipedRadioButton.Checked Then
    219. endLocation = e.Location
    220. Dim s As Point
    221. s.X = endLocation.X - startLocation.X
    222. If s.X < 0 Then
    223. Dim tmp As Integer = startLocation.X
    224. startLocation.X = endLocation.X
    225. endLocation.X = tmp
    226. End If
    227. s.Y = endLocation.Y - startLocation.Y
    228. If s.Y < 0 Then
    229. Dim tmp As Integer = startLocation.Y
    230. startLocation.Y = endLocation.Y
    231. endLocation.Y = tmp
    232. End If
    233. s.X = Math.Abs(s.X)
    234. s.Y = Math.Abs(s.Y)
    235. Dim p(3) As Point
    236. p(0) = New Point(startLocation.X + s.X / 5, startLocation.Y)
    237. p(1) = New Point(startLocation.X + s.X, startLocation.Y)
    238. p(2) = New Point(endLocation.X - s.X / 5, endLocation.Y)
    239. p(3) = New Point(endLocation.X - s.X, endLocation.Y)
    240. g.DrawPolygon(PenPoint, p)
    241. ElseIf FillRadioButton.Checked Then
    242. FillRegion(e.X, e.Y, CurrentColor)
    243. ElseIf TextRadioButton.Checked Then
    244. Dim txt As String = Me.TextDrawTextBox.Text
    245. g.DrawString(txt, CurrentFont, New Drawing2D.HatchBrush(Drawing2D.HatchStyle.BackwardDiagonal, CurrentColor), e.X, e.Y)
    246. End If
    247. End If
    248. drawing = False
    249. UpdateImage()
    250. End Sub
    251. Private Sub PaintForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    252. CurrentFont = Me.Font
    253. FontButton.Text = CurrentFont.ToString
    254. Me.ColorButton.BackColor = Color.Blue
    255. Me.Color2Button.BackColor = Color.LightSkyBlue
    256. g = Graphics.FromImage(LastImage)
    257. g.Clear(Color.White)
    258. UpdateImage()
    259. ReloadPen(PenWidth, CurrentColor)
    260. End Sub
    261. Private Sub ColorButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ColorButton.Click
    262. If ColorDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
    263. CurrentColor = ColorDialog1.Color
    264. Me.ColorButton.BackColor = CurrentColor
    265. ReloadPen(PenWidth, CurrentColor)
    266. End If
    267. End Sub
    268. Private Sub Color2Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Color2Button.Click
    269. If ColorDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
    270. CurrentColor2 = ColorDialog1.Color
    271. Me.Color2Button.BackColor = CurrentColor2
    272. End If
    273. End Sub
    274. Private Sub ClearToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ClearToolStripMenuItem.Click
    275. ClearScreen()
    276. End Sub
    277. Sub ClearScreen()
    278. 'LastImage = New Bitmap(501, 501)
    279. g = Graphics.FromImage(LastImage)
    280. g.Clear(Color.White)
    281. UpdateImage()
    282. End Sub
    283. Private Sub SaveToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveToolStripMenuItem.Click
    284. If SavedFileAddress = "" Then
    285. SaveFileDialog1.Filter = "Bitmap File (*.bmp)|*.bmp"
    286. If SaveFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
    287. SavedFileAddress = SaveFileDialog1.FileName
    288. LastImage.Save(SavedFileAddress)
    289. Me.Text = SaveFileDialog1.FileName
    290. End If
    291. Else
    292. LastImage.Save(SavedFileAddress)
    293. Me.Text = SaveFileDialog1.FileName
    294. End If
    295. End Sub
    296. Private Sub SaveAsToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveAsToolStripMenuItem.Click
    297. SaveFileDialog1.Filter = "Bitmap File (*.bmp)|*.bmp"
    298. If SaveFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
    299. SavedFileAddress = SaveFileDialog1.FileName
    300. LastImage.Save(SavedFileAddress)
    301. Me.Text = SaveFileDialog1.FileName
    302. End If
    303. End Sub
    304. Private Sub TriangleRadioButton_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TriangleRadioButton.CheckedChanged
    305. TempLocation = New Point(-1, -1)
    306. End Sub
    307. Private Sub P1RadioButton_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles P1RadioButton.CheckedChanged
    308. PenWidth = 1.0F
    309. ReloadPen(PenWidth, CurrentColor)
    310. End Sub
    311. Private Sub P2RadioButton_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles P2RadioButton.CheckedChanged
    312. PenWidth = 5.0F
    313. ReloadPen(PenWidth, CurrentColor)
    314. End Sub
    315. Private Sub P3RadioButton_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles P3RadioButton.CheckedChanged
    316. PenWidth = 10.0F
    317. ReloadPen(PenWidth, CurrentColor)
    318. End Sub
    319. Private Sub M1ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles M1ToolStripMenuItem.Click
    320. M1 = LastImage.Clone()
    321. End Sub
    322. Private Sub M2ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles M2ToolStripMenuItem.Click
    323. M2 = LastImage.Clone()
    324. End Sub
    325. Private Sub M3ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles M3ToolStripMenuItem.Click
    326. M3 = LastImage.Clone()
    327. End Sub
    328. Private Sub M4ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles M4ToolStripMenuItem.Click
    329. M4 = LastImage.Clone()
    330. End Sub
    331. Private Sub S1ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles S1ToolStripMenuItem.Click
    332. LastImage = M1.Clone
    333. UpdateImage()
    334. End Sub
    335. Private Sub S2ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles S2ToolStripMenuItem.Click
    336. LastImage = M2.Clone
    337. UpdateImage()
    338. End Sub
    339. Private Sub S3ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles S3ToolStripMenuItem.Click
    340. LastImage = M3.Clone
    341. UpdateImage()
    342. End Sub
    343. Private Sub S4ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles S4ToolStripMenuItem.Click
    344. LastImage = M4.Clone
    345. UpdateImage()
    346. End Sub
    347. Private Sub NewToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NewToolStripMenuItem.Click
    348. If SavedFileAddress = "" Then
    349. Dim result As Integer = MsgBox("Would you like to save this picture", MsgBoxStyle.YesNo)
    350. If result = MsgBoxResult.Yes Then
    351. SaveFileDialog1.Filter = "Bitmap File (*.bmp)|*.bmp"
    352. If SaveFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
    353. SavedFileAddress = SaveFileDialog1.FileName
    354. LastImage.Save(SavedFileAddress)
    355. Me.Text = SaveFileDialog1.FileName
    356. End If
    357. End If
    358. End If
    359. g.Clear(Color.White)
    360. UpdateImage()
    361. SavedFileAddress = ""
    362. End Sub
    363. Private Sub ExitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitToolStripMenuItem.Click
    364. If SavedFileAddress = "" Then
    365. Dim result As Integer = MsgBox("Would you like to save the image before exiting?", MsgBoxStyle.YesNoCancel)
    366. If result = MsgBoxResult.Yes Then
    367. SaveFileDialog1.Filter = "Bitmap File (*.bmp)|*.bmp"
    368. If SaveFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
    369. SavedFileAddress = SaveFileDialog1.FileName
    370. LastImage.Save(SavedFileAddress)
    371. Me.Text = SaveFileDialog1.FileName
    372. End If
    373. ElseIf result = MsgBoxResult.No Then
    374. End
    375. End If
    376. End If
    377. End Sub
    378. Private Sub TextRadioButton_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextRadioButton.CheckedChanged
    379. If Me.TextRadioButton.Checked = True Then
    380. Me.TextDrawTextBox.Visible = True
    381. FontButton.Visible = True
    382. Else
    383. Me.TextDrawTextBox.Visible = False
    384. FontButton.Visible = False
    385. End If
    386. End Sub
    387. Private Sub FontButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FontButton.Click
    388. If FontDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
    389. CurrentFont = FontDialog1.Font
    390. FontButton.Text = CurrentFont.ToString
    391. End If
    392. End Sub
    393. Private Sub MessageLabel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
    394. End Sub
    395. End Class

    @Visual_Prog Du musst allerdings einen Pfad und einen Namen vorhalten. Den bekommst Du, wenn das Bild das erste Mal gespeichert wurde oder wenn das Bild geladen wurde.
    Und:
    Gib Deinem Projekt Option Strict On.
    Falls Du diesen Code kopierst, achte auf die C&P-Bremse.
    Jede einzelne Zeile Deines Programms, die Du nicht explizit getestet hast, ist falsch :!:
    Ein guter .NET-Snippetkonverter (der ist verfügbar).
    Programmierfragen über PN / Konversation werden ignoriert!