vb.net - Drawing and saving a VB .net Graphic as a file - Stack Overflow

I have a short example application using VB3.5 and Windows Forms. I must admit, that VB is not my fav

I have a short example application using VB 3.5 and Windows Forms. I must admit, that VB is not my favourite language, but I must use them here. I was looking at different other questions Drawing to and saving a png in vb and Saving drawn images in a PictureBox, but they did not seem to fit my problem or are too short for me understanding it.

Problem Description

I have to write a short stand-alone Windows application, that displays some easy graphics. These graphics must be stored on a drive. There are two points I do not get running as I want:

  1. If my application starts up, the Form1_Load() is executed. I can see in debugging mode, that it runs through the commands. At the end I would expect to have the graphics representation of horizontal lines in the PictureBox1, but there is none. The form is displayed, but empty.

Only if I click with the left mouse button on PictureBox1 there is an image visible. In both cases the drawgraph() routine is executed. Why doesn't it show the same content after Form1_Load() and after MouseClick event?

  1. When right clicking on the PictureBox1, a file name dialog pops up in order to be able to save the file as bitmap image. A file is saved as image file, it has the same size and proper file type, but it is just a plain white image. How can the content of the PictureBox1 be saved to file? As I understand, the Graphics object is needed to handle the DrawLine() output, that is then displayed in the PictureBox1. When saving the contents from PictureBox1, the content has to be transferred to a Bitmap object, which can then be saved.

Code

Public Class Form1
Public graph As Graphics
Public pen As Pen

Sub drawgraph()         'Draw horizontal lines
 Dim i As Integer
 graph.Clear(Color.White)
 i = 0
 pen.Color = Color.Green
 Do
  i = i + 10
  graph.DrawLine(pen, 0, i, PictureBox1.Width, i)
 Loop Until i > PictureBox1.Height
End Sub

Private Sub PictureBox1_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseClick
 Dim dlg As New SaveFileDialog()
 Dim bmp As New Bitmap(PictureBox1.Width, PictureBox1.Height, System.Drawing.Imaging.PixelFormat.Format24bppRgb)
 Dim s As String

 If e.Button = Windows.Forms.MouseButtons.Left Then       ' This shows and updates the graph
  drawgraph()        

 ElseIf e.Button = Windows.Forms.MouseButtons.Right Then  ' Saving as file not working
  dlg.Filter = "Portable Network File PNG|*.png"
  If dlg.ShowDialog = Windows.Forms.DialogResult.OK Then
   Update()
   s = dlg.FileName
   PictureBox1.DrawToBitmap(bmp, New System.Drawing.Rectangle(0, 0, bmp.Width, bmp.Height))
   bmp.Save(s, System.Drawing.Imaging.ImageFormat.Png)
   bmp = Nothing
  End If
 End If

End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
 graph = PictureBox1.CreateGraphics()
 pen = New Pen(Color.Black, 1)
 drawgraph()      ' This is executed on load, but does not show anything
End Sub

End Class

Question

How can the content of the graphics be displayed directly after loading the form?

What went wrong when trying to save the graphics? Is my assumption of data flow (Graphics -> PictureBox -> Bitmap) wrong?

I have a short example application using VB 3.5 and Windows Forms. I must admit, that VB is not my favourite language, but I must use them here. I was looking at different other questions Drawing to and saving a png in vb and Saving drawn images in a PictureBox, but they did not seem to fit my problem or are too short for me understanding it.

Problem Description

I have to write a short stand-alone Windows application, that displays some easy graphics. These graphics must be stored on a drive. There are two points I do not get running as I want:

  1. If my application starts up, the Form1_Load() is executed. I can see in debugging mode, that it runs through the commands. At the end I would expect to have the graphics representation of horizontal lines in the PictureBox1, but there is none. The form is displayed, but empty.

Only if I click with the left mouse button on PictureBox1 there is an image visible. In both cases the drawgraph() routine is executed. Why doesn't it show the same content after Form1_Load() and after MouseClick event?

  1. When right clicking on the PictureBox1, a file name dialog pops up in order to be able to save the file as bitmap image. A file is saved as image file, it has the same size and proper file type, but it is just a plain white image. How can the content of the PictureBox1 be saved to file? As I understand, the Graphics object is needed to handle the DrawLine() output, that is then displayed in the PictureBox1. When saving the contents from PictureBox1, the content has to be transferred to a Bitmap object, which can then be saved.

Code

Public Class Form1
Public graph As Graphics
Public pen As Pen

Sub drawgraph()         'Draw horizontal lines
 Dim i As Integer
 graph.Clear(Color.White)
 i = 0
 pen.Color = Color.Green
 Do
  i = i + 10
  graph.DrawLine(pen, 0, i, PictureBox1.Width, i)
 Loop Until i > PictureBox1.Height
End Sub

Private Sub PictureBox1_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseClick
 Dim dlg As New SaveFileDialog()
 Dim bmp As New Bitmap(PictureBox1.Width, PictureBox1.Height, System.Drawing.Imaging.PixelFormat.Format24bppRgb)
 Dim s As String

 If e.Button = Windows.Forms.MouseButtons.Left Then       ' This shows and updates the graph
  drawgraph()        

 ElseIf e.Button = Windows.Forms.MouseButtons.Right Then  ' Saving as file not working
  dlg.Filter = "Portable Network File PNG|*.png"
  If dlg.ShowDialog = Windows.Forms.DialogResult.OK Then
   Update()
   s = dlg.FileName
   PictureBox1.DrawToBitmap(bmp, New System.Drawing.Rectangle(0, 0, bmp.Width, bmp.Height))
   bmp.Save(s, System.Drawing.Imaging.ImageFormat.Png)
   bmp = Nothing
  End If
 End If

End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
 graph = PictureBox1.CreateGraphics()
 pen = New Pen(Color.Black, 1)
 drawgraph()      ' This is executed on load, but does not show anything
End Sub

End Class

Question

How can the content of the graphics be displayed directly after loading the form?

What went wrong when trying to save the graphics? Is my assumption of data flow (Graphics -> PictureBox -> Bitmap) wrong?

Share Improve this question edited Nov 18, 2024 at 14:34 Jimi 32.4k8 gold badges50 silver badges78 bronze badges asked Nov 18, 2024 at 12:44 destructioneerdestructioneer 3924 silver badges14 bronze badges 2
  • The accepted answer to VB.NET drawing multiple lines on a PictureBox without removing previous changes might contain everything you need to know about creating the image. – Andrew Morton Commented Nov 18, 2024 at 13:27
  • 2 You need to avoid this: graph = PictureBox1.CreateGraphics(). You cannot store the Graphics object, it becomes invalid as soon as the Control is repainted (aka invalidated). The Paint event of a Control provides the Graphics object you need. See the DrawSignature() method here or the Paint event of a Control here – Jimi Commented Nov 18, 2024 at 14:30
Add a comment  | 

1 Answer 1

Reset to default 0

Use a Bitmap object as the backing store for the graphics. Assign this Bitmap to PictureBox1.Image. Draw on this Bitmap using a Graphics object.

Since the PictureBox1.Image will now contain the drawn content, saving the PictureBox1.Image directly will resolve the issue.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745617503a4636323.html

相关推荐

  • vb.net - Drawing and saving a VB .net Graphic as a file - Stack Overflow

    I have a short example application using VB3.5 and Windows Forms. I must admit, that VB is not my fav

    9小时前
    30

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信