Bitmap image = new Bitmap(); // You can create a new Bitmap or get it from disk. //Creates Graphics from the image. Graphics g = Graphics.FromImage(image); //You can operate the g to draw something on the image. //You can save the iamge to disk. image.Save("test.bmp", ImageFormat.Bmp);
不知LZ对vb的代码理解的如何,我用下面这种方式试了试,可以实现在picturebox中绘制好图形后保存成bmp文件,然后以后还可以重新加载这幅bmp图至picturebox中,然后继续绘制图像,再进行保存。 form上需要一个名为Pic1的的Picturebox控件,两个按钮:button1保存绘制的图形,button2加载图像。不过第二次继续绘制图像后不要再保存覆盖原先的文件,可以另存为一个其他文件,否则出错:Gdi+出现一般性错误。Imports System.Drawing Imports System.Drawing.Drawing2DPublic Class form1 Dim PtStart, pt As Point Dim RectSize As Size Dim bmp As Bitmap Private Sub Pic1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Pic1.MouseDown If e.Button = Windows.Forms.MouseButtons.Left Then PtStart = New Point(e.X, e.Y) End Sub Private Sub Pic1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Pic1.MouseMove If e.Button = Windows.Forms.MouseButtons.Left Then Dim p As Point = PointToScreen(PtStart) : p.Offset(Pic1.Location) If pt <> Nothing Then ControlPaint.DrawReversibleFrame(New Rectangle(p.X, p.Y, pt.X - p.X, pt.Y - p.Y), Color.Red, FrameStyle.Dashed) pt = PointToScreen(New Point(e.X, e.Y)) : pt.Offset(Pic1.Location) ControlPaint.DrawReversibleFrame(New Rectangle(p.X, p.Y, pt.X - p.X, pt.Y - p.Y), Color.Red, FrameStyle.Dashed) End If End Sub Private Sub DrawRect(ByVal mPoint1 As Point, ByVal mPoint2 As Point) If mPoint2.X > mPoint1.X And mPoint2.Y > mPoint1.Y Then PtStart = New Point(mPoint1.X, mPoint1.Y) : RectSize = New Size(mPoint2.X - mPoint1.X, mPoint2.Y - mPoint1.Y) ElseIf mPoint2.X > mPoint1.X And mPoint2.Y < mPoint1.Y Then PtStart = New Point(mPoint1.X, mPoint2.Y) : RectSize = New Size(mPoint2.X - mPoint1.X, mPoint1.Y - mPoint2.Y) ElseIf mPoint2.X < mPoint1.X And mPoint2.Y > mPoint1.Y Then PtStart = New Point(mPoint2.X, mPoint1.Y) : RectSize = New Size(mPoint1.X - mPoint2.X, mPoint2.Y - mPoint1.Y) ElseIf mPoint2.X < mPoint1.X And mPoint2.Y < mPoint1.Y Then PtStart = New Point(mPoint2.X, mPoint2.Y) : RectSize = New Size(mPoint1.X - mPoint2.X, mPoint1.Y - mPoint2.Y) End If Graphics.FromImage(bmp).DrawRectangle(Pens.Blue, New Rectangle(PtStart, RectSize)) Pic1.Refresh() End Sub Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load Me.SetStyle(ControlStyles.AllPaintingInWmPaint Or ControlStyles.OptimizedDoubleBuffer Or ControlStyles.UserPaint, True) bmp = New Bitmap(Pic1.Width, Pic1.Height) Pic1.Image = bmp End Sub Private Sub Pic1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Pic1.MouseUp If e.Button = Windows.Forms.MouseButtons.Left Then Dim p As Point = PointToScreen(PtStart) : p.Offset(Pic1.Location) ControlPaint.DrawReversibleFrame(New Rectangle(p.X, p.Y, pt.X - p.X, pt.Y - p.Y), Color.Red, FrameStyle.Dashed) Call DrawRect(PtStart, New Point(e.X, e.Y)) pt = Nothing End If End Sub Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click Dim fileSaveDialog As New SaveFileDialog fileSaveDialog.Filter = "bmp(*.bmp)|*.bmp" If fileSaveDialog.ShowDialog = Windows.Forms.DialogResult.OK Then bmp.Save(fileSaveDialog.FileName) End If End Sub Private Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click Dim fileOpenDialog As New OpenFileDialog fileOpenDialog.Filter = "bmp(*.bmp)|*.bmp" If fileOpenDialog.ShowDialog = Windows.Forms.DialogResult.OK Then bmp = New Bitmap(fileOpenDialog.FileName) Pic1.Image = bmp End If End Sub End Class
调出来的图象是不能接着画的 变成图象格式程序不能解析出来了
呵呵 要是能解析出来 那就是现在最牛的图形图象处理了,比ADO牛比了
Bitmap image = new Bitmap();
// You can create a new Bitmap or get it from disk. //Creates Graphics from the image.
Graphics g = Graphics.FromImage(image); //You can operate the g to draw something on the image.
//You can save the iamge to disk.
image.Save("test.bmp", ImageFormat.Bmp);
开始我存成格式1.jif
使用代码:img.save("C:\\1.gif",imageFormat.Gif)
可是再次调用1.gif时,却提示无法转换成 Graphics格式
gph = Graphics.FromImage(img);
也就是说,我想将下一次的绘图建立在第一次的基础上!
求教!!
首先谢谢你的帮助,
那么怎么将 Graphics g存放在一个bmp图中呢?
是不是真的像车神所说的那样变成图片就无法再操作了呢?
求助达人,怎么会在一个For循环中
图像不断的在原基础上更新?
是不是真的像车神所说的那样变成图片就无法再操作了呢?
===========================================================
声明了之后就直接操作就可以了, 就在bitmap上绘图了.for循环也是一样的 在for的外面定义image, 循环的绘图就可以了.
如果g来自Graphics.FromImage(image),那么对g的任何操作事实上都等于是在image上画,所以画完后不用考虑怎么保存g,只要image.Save,g上新画的东西就留在image中了
form上需要一个名为Pic1的的Picturebox控件,两个按钮:button1保存绘制的图形,button2加载图像。不过第二次继续绘制图像后不要再保存覆盖原先的文件,可以另存为一个其他文件,否则出错:Gdi+出现一般性错误。Imports System.Drawing
Imports System.Drawing.Drawing2DPublic Class form1
Dim PtStart, pt As Point
Dim RectSize As Size
Dim bmp As Bitmap Private Sub Pic1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Pic1.MouseDown
If e.Button = Windows.Forms.MouseButtons.Left Then PtStart = New Point(e.X, e.Y)
End Sub Private Sub Pic1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Pic1.MouseMove
If e.Button = Windows.Forms.MouseButtons.Left Then
Dim p As Point = PointToScreen(PtStart) : p.Offset(Pic1.Location)
If pt <> Nothing Then ControlPaint.DrawReversibleFrame(New Rectangle(p.X, p.Y, pt.X - p.X, pt.Y - p.Y), Color.Red, FrameStyle.Dashed)
pt = PointToScreen(New Point(e.X, e.Y)) : pt.Offset(Pic1.Location)
ControlPaint.DrawReversibleFrame(New Rectangle(p.X, p.Y, pt.X - p.X, pt.Y - p.Y), Color.Red, FrameStyle.Dashed)
End If
End Sub Private Sub DrawRect(ByVal mPoint1 As Point, ByVal mPoint2 As Point)
If mPoint2.X > mPoint1.X And mPoint2.Y > mPoint1.Y Then
PtStart = New Point(mPoint1.X, mPoint1.Y) : RectSize = New Size(mPoint2.X - mPoint1.X, mPoint2.Y - mPoint1.Y)
ElseIf mPoint2.X > mPoint1.X And mPoint2.Y < mPoint1.Y Then
PtStart = New Point(mPoint1.X, mPoint2.Y) : RectSize = New Size(mPoint2.X - mPoint1.X, mPoint1.Y - mPoint2.Y)
ElseIf mPoint2.X < mPoint1.X And mPoint2.Y > mPoint1.Y Then
PtStart = New Point(mPoint2.X, mPoint1.Y) : RectSize = New Size(mPoint1.X - mPoint2.X, mPoint2.Y - mPoint1.Y)
ElseIf mPoint2.X < mPoint1.X And mPoint2.Y < mPoint1.Y Then
PtStart = New Point(mPoint2.X, mPoint2.Y) : RectSize = New Size(mPoint1.X - mPoint2.X, mPoint1.Y - mPoint2.Y)
End If
Graphics.FromImage(bmp).DrawRectangle(Pens.Blue, New Rectangle(PtStart, RectSize))
Pic1.Refresh()
End Sub Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Me.SetStyle(ControlStyles.AllPaintingInWmPaint Or ControlStyles.OptimizedDoubleBuffer Or ControlStyles.UserPaint, True)
bmp = New Bitmap(Pic1.Width, Pic1.Height)
Pic1.Image = bmp
End Sub Private Sub Pic1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Pic1.MouseUp
If e.Button = Windows.Forms.MouseButtons.Left Then
Dim p As Point = PointToScreen(PtStart) : p.Offset(Pic1.Location)
ControlPaint.DrawReversibleFrame(New Rectangle(p.X, p.Y, pt.X - p.X, pt.Y - p.Y), Color.Red, FrameStyle.Dashed)
Call DrawRect(PtStart, New Point(e.X, e.Y))
pt = Nothing
End If
End Sub Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim fileSaveDialog As New SaveFileDialog
fileSaveDialog.Filter = "bmp(*.bmp)|*.bmp"
If fileSaveDialog.ShowDialog = Windows.Forms.DialogResult.OK Then
bmp.Save(fileSaveDialog.FileName)
End If
End Sub Private Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim fileOpenDialog As New OpenFileDialog
fileOpenDialog.Filter = "bmp(*.bmp)|*.bmp"
If fileOpenDialog.ShowDialog = Windows.Forms.DialogResult.OK Then
bmp = New Bitmap(fileOpenDialog.FileName)
Pic1.Image = bmp
End If
End Sub
End Class