将图形保存到位图文件 [英] Saving a Drawing to a Bitmap File
问题描述
在VB 2010中:
如何将图片框上的图形保存到硬盘上的文件中?
In VB 2010:
How do i save a drawing on a picturebox to a file on harddisk
Imports System.Drawing.Imaging
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
myPictureBox.BackColor = Color.Yellow
myPictureBox.Image = Image.FromFile("C:\TEMP\test.jpg")
End Sub
Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
myPictureBox.Image.Save("c:\Temp\MyPicture.jpg", ImageFormat.Jpeg)
End Sub
Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
Dim redPen As Pen = New Pen(Color.Red, 20)
Dim point1 As Point = New Point(0, 0)
Dim point2 As Point = New Point(130, 150)
Dim G As Graphics
G = myPictureBox.CreateGraphics
G.DrawLine(redPen, point1, point2)
End Sub
End Class
保存命令(Button1)不起作用.
我得到了消息:
使用New关键字创建实例.
当我在myPictureBox中加载图像时,
就像我现在在该行中所做的那样:
myPictureBox.Image = Image.FromFile("C:\ TEMP \ test.jpg")
保存命令有效,但结果不是我想要的.
我想在图像上加上红线.
(我正在尝试做的是为孩子制作一个程序,以在加载的位图上赋予颜色,
和存储彩色位图的可能性)
the save command ( Button1) did not work.
I got the mesage:
Use the New keyword to create the instance.
when I load an image in myPictureBox,
as I now did in the line:
myPictureBox.Image = Image.FromFile("C:\TEMP\test.jpg")
The Save command worked,but the result is not what I want.
I wanted the redline ON my image.
(What I am trying to do is making a program for a child to give color on an loaded bitmap,
and the possebility to store the colored bitmap)
推荐答案
尝试:
myPictureBox.Image.Save("D:\Temp\MyPicture.jpg", ImageFormat.Jpeg)
如果您需要保存PictureBox
的图像,则可能会误用该类.通常,您根本不需要它.只展示一些图片是很好的.在更复杂的情况下,它无济于事,只会增加一些麻烦.
我在过去的答案中解释了为什么以及该怎么做:
如何从旧图纸中清除面板 [ ^ ],
在C#中绘制矩形 [
If you have a need to save an image of thePictureBox
, the chances are, you misuse this class. Usually, you don''t need it at all. It is good only to present some picture; in more complex cases it does not help, only adds some hassles.
I explained why and what to do instead in my past answers:
How do I clear a panel from old drawing[^],
draw a rectangle in C#[^].
Just in case, please review it.
—SA
这篇关于将图形保存到位图文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!