admin管理员组文章数量:1434239
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:
- 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 thePictureBox1
, 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?
- 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 thePictureBox1
be saved to file? As I understand, theGraphics
object is needed to handle theDrawLine()
output, that is then displayed in thePictureBox1
. When saving the contents fromPictureBox1
, the content has to be transferred to aBitmap
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:
- 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 thePictureBox1
, 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?
- 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 thePictureBox1
be saved to file? As I understand, theGraphics
object is needed to handle theDrawLine()
output, that is then displayed in thePictureBox1
. When saving the contents fromPictureBox1
, the content has to be transferred to aBitmap
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?
1 Answer
Reset to default 0Use 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.
本文标签: vbnetDrawing and saving a VB net Graphic as a fileStack Overflow
版权声明:本文标题:vb.net - Drawing and saving a VB .net Graphic as a file - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745617502a2666488.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
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 theDrawSignature()
method here or thePaint
event of a Control here – Jimi Commented Nov 18, 2024 at 14:30