亲们,在picturebox里面,运行代码之后,画了条曲线,为什么没有办法保存成图片格式呢???
这是我的代码,帮我分析一下,保存完后是张空白,我纠结好长时间了,又一个上午过去了,帮我看看,想想办法!
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Linq
Imports System.Text
Imports System.Windows.Forms
Public Class Form1
Inherits Form
Public Sub New()
InitializeComponent()
End Sub
'定义画线的两个点p1起始点p2终点
Private p1 As Point, p2 As Point
Public image As Bitmap = Nothing
'窗体载入时新建一个绘图对象
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Me.PictureBox1.Controls.Clear()
image = New Bitmap(Me.PictureBox1.Width, Me.PictureBox1.Height)
Graphics.FromImage(image).Clear(Color.White)
'消除底图的黑色
Me.PictureBox1.Image = DirectCast(image.Clone(), Bitmap)
'这句话是关键
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim sf As New SaveFileDialog()
sf.Filter = "(*.bmp)|*.bmp|(*.jpg)|*.jpg"
sf.FilterIndex = 2
sf.RestoreDirectory = True
If sf.ShowDialog() = DialogResult.OK Then
Dim bmp As New Bitmap(PictureBox1.Size.Width, PictureBox1.Size.Height)
PictureBox1.DrawToBitmap(bmp, New Rectangle(New Point(0, 0), PictureBox1.Size))
bmp.Save(sf.FileName, System.Drawing.Imaging.ImageFormat.Jpeg)
End If
End Sub
Private Sub PictureBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown
If e.Button = Windows.Forms.MouseButtons.Left Then
p1.X = e.X
p1.Y = e.Y
End If
End Sub
Private Sub PictureBox1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseMove
Dim g As Graphics = PictureBox1.CreateGraphics()
'如果鼠标左键一直接下
If e.Button = Windows.Forms.MouseButtons.Left Then
p2.X = e.X
p2.Y = e.Y
g.DrawLine(New Pen(Color.Blue, 2), p1, p2)
p1 = p2
End If
End Sub
' Private Sub PictureBox1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseUp
'Dim g As Graphics = PictureBox1.CreateGraphics()
'If e.Button = Windows.Forms.MouseButtons.Left Then
' p2.X = e.X
' p2.Y = e.Y
' g.DrawLine(New Pen(Color.Blue, 2), p1, p2)
'End If
'End Sub
End Class
[解决办法]
改为(红色部分)
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Dim sf As New SaveFileDialog()
sf.Filter = "(*.bmp)