代码如下
public Graphics g;
public Form1()
{
InitializeComponent();
} private void Form1_Paint(object sender, PaintEventArgs e)
{
g = e.Graphics;
g.FillEllipse(new SolidBrush(Color.Red), 5, 5, 200, 100);
g.DrawRectangle(new Pen(Color.Blue, 4.0f), 230, 5, 150, 100);
Font font = new Font("楷体_GB2313", 32);
g.TextRenderingHint = TextRenderingHint.AntiAlias;
g.DrawString("点击按扭保存图像", font, new SolidBrush(Color.Blue), 15, 110); } private void save_Click(object sender, EventArgs e)
{
Bitmap image = new Bitmap(this.Width, this.Height,this.g);//此名有问题,参数无效。那如何解决,
//Graphics imageG = Graphics.FromImage(image);
//imageG.Clear(this.BackColor);
//Form1_Paint(this, new PaintEventArgs(imageG, this.ClientRectangle));
image.Save("saveform.jpg", ImageFormat.Jpeg);
}
public Graphics g;
public Form1()
{
InitializeComponent();
} private void Form1_Paint(object sender, PaintEventArgs e)
{
g = e.Graphics;
g.FillEllipse(new SolidBrush(Color.Red), 5, 5, 200, 100);
g.DrawRectangle(new Pen(Color.Blue, 4.0f), 230, 5, 150, 100);
Font font = new Font("楷体_GB2313", 32);
g.TextRenderingHint = TextRenderingHint.AntiAlias;
g.DrawString("点击按扭保存图像", font, new SolidBrush(Color.Blue), 15, 110); } private void save_Click(object sender, EventArgs e)
{
Bitmap image = new Bitmap(this.Width, this.Height,this.g);//此名有问题,参数无效。那如何解决,
//Graphics imageG = Graphics.FromImage(image);
//imageG.Clear(this.BackColor);
//Form1_Paint(this, new PaintEventArgs(imageG, this.ClientRectangle));
image.Save("saveform.jpg", ImageFormat.Jpeg);
}
一、Bitmap image = new Bitmap(this.Width, this.Height,this.g)改为
Bitmap image = new Bitmap(this.Width, this.Height)二、在Bitmap image = new Bitmap(this.Width, this.Height,this.g)前加上
g = this.CreateGraphics();
在保存前,把Graphics创建为PaintEventArgs去调用Form1_Paint
{
g = e.Graphics;
}//在Form1_Paint中的e.Graphics,出来这个函数后e.Graphics就不能用了。
//把 Bitmap image = new Bitmap(this.Width, this.Height,this.g)
//这句放到Form1_Paint中试试。
明白在//Form1_Paint(this, new PaintEventArgs(imageG, this.ClientRectangle));
这句做了什么,与下面的写法区别在哪
回楼上你的方法我早就试过如下,还是不行,要不你们自己试一下
public partial class Form1 : Form
{
public bool msave;
public Form1()
{
InitializeComponent();
msave = false;
} private void Form1_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
g.FillEllipse(new SolidBrush(Color.Red), 5, 5, 200, 100);
g.DrawRectangle(new Pen(Color.Blue, 4.0f), 230, 5, 150, 100);
Font font = new Font("楷体_GB2313", 32);
g.TextRenderingHint = TextRenderingHint.AntiAlias;
g.DrawString("点击按扭保存图像", font, new SolidBrush(Color.Blue), 15, 110);
if (msave)
{
msave = false;
Bitmap image = new Bitmap(this.Width, this.Height, g);
image.Save("123.jpg", ImageFormat.Jpeg);
} } private void save_Click(object sender, EventArgs e)
{
msave =true;
this.Invalidate(this.ClientRectangle);
}
}
也就是你要定义全局的
image.Save("123.jpg", ImageFormat.Jpeg);
你的image有内容吗,没有.
你再添加一句this.BackgroundImage = image可以看到更奇特的现象,呵呵
这个image是没有图像的
Graphics dc=Graphics.FromImage;
用dc画图
[System.Runtime.InteropServices.DllImport("gdi32.dll")]
public static extern long BitBlt (IntPtr hdcDest, int nXDest, int nYDest, int nWidth, int nHeight,IntPtr hdcSrc,int nXSrc, int nYSrc, int dwRop);
private Bitmap memoryImage;
private void CaptureScreen()
{
Graphics mygraphics = this.CreateGraphics();
Size s = this.Size;
memoryImage = new Bitmap(s.Width, s.Height, mygraphics);
Graphics memoryGraphics = Graphics.FromImage(memoryImage);
IntPtr dc1 = mygraphics.GetHdc();
IntPtr dc2 = memoryGraphics.GetHdc();
BitBlt(dc2, 0, 0, this.ClientRectangle.Width, this.ClientRectangle.Height, dc1, 0, 0, 13369376);
mygraphics.ReleaseHdc(dc1);
memoryGraphics.ReleaseHdc(dc2);
memoryImage.Save(@"c:\1243.jpg", ImageFormat.Jpeg);
}
把memoryImage = new Bitmap(s.Width, s.Height, mygraphics);改成
memoryImage = new Bitmap(this.ClientRectangle.Width,this.ClientRectangle.Height, mygraphics);
呵呵你用的是VC中的思想吧,还是C#本来就有这种方法...................