using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Drawing.Imaging; using System.Text; using System.Windows.Forms;namespace testsaveimage { public partial class Form1 : Form {
public Form1() { InitializeComponent(); } int i = 5;//画线用的 System.Drawing.Bitmap image = null;//建立一个实例 private void button1_Click(object sender, EventArgs e) {
Graphics g = Graphics.FromImage(pictureBox1.Image); g.DrawLine(Pens.Gold,10+i,10,110,10+i);
(.net下支持的图像格式很多的哦)
2楼的方法我试过,不行,因为我并没有设置PictureBox的Image属性,而是直接在其上画的图,所以其Image为null.不过还是要谢谢你的热心帮助^_^.
我刚想到了一种变通的方法,就是先对一空白的图片进行绘制,然后再将其赋予PictureBox的Image属性来显示,然后再保存。感觉该方法虽然行的通,但过于麻烦,不知能否直接对PictureBox进行绘图然后保存?
Graphics g=Graphics.FromImage(image);
Rectangle rect=new Rectangle(0, 0,100, 100);
paint(pictureBox1,new PaintEventArgs(g,rect));
//paint()为画图程序
try
{
image.Save("333.jpg",ImageFormat.Jpeg);
}
catch(Exception exception1)
{
MessageBox.Show(exception1.Message);
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Text;
using System.Windows.Forms;namespace testsaveimage
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} int i = 5;//画线用的
System.Drawing.Bitmap image = null;//建立一个实例
private void button1_Click(object sender, EventArgs e)
{
Graphics g = Graphics.FromImage(pictureBox1.Image); g.DrawLine(Pens.Gold,10+i,10,110,10+i);
i += 10;
this.pictureBox1.Refresh();
g.Dispose();
} private void button2_Click(object sender, EventArgs e)
{
this.pictureBox1.Image.Save(@"c:\test.jpg", ImageFormat.Gif); } private void Form1_Load(object sender, EventArgs e)
{
this.pictureBox1.Controls.Clear();
image = new Bitmap(this.pictureBox1.Width, this.pictureBox1.Height);
Graphics.FromImage(image).Clear(Color.White);//消除底图的黑色
this.pictureBox1.Image = (Bitmap)image.Clone();//这句话是关键
} }
}
点击button1画线,点击button2保存。上面代码使用vs2005写的
我这还有一个很好的实例,是从www.codeproject.com下的,需要的话可以给你发过去