【发布时间】:2014-06-28 05:51:42
【问题描述】:
我正在用 C# VS2010 开发一个绘图应用程序
启动界面为空白pictureBox,
我为画笔工具绘制做了一个mouseDown 和一个mouseMove 事件处理程序
而且效果很好
当我尝试保存新图片时(在空白图片框上绘画后) 我输入文件,它只是一张空白图片。
问题是代码没有保存效果。
为什么?
mouseDown 事件处理程序
private void mouseDown(object sender, MouseEventArgs e)
{
if (CurrentFunction == "DrawFree")
{
if (e.Button == MouseButtons.Left)
ReleaseFlag = true;
StartPoint = e.Location;
}
}
mouseMove 事件处理程序
private void mouseMove(object sender, MouseEventArgs e)
{
if (CurrentFunction == "DrawFree")
{
if (ReleaseFlag)
{
EndPoint = e.Location;
g = pictureBox1.CreateGraphics();
g.DrawLine(p, StartPoint, EndPoint);
}
StartPoint = EndPoint;
}
}
保存代码
private void savePhotoToolStripMenuItem_Click(object sender, EventArgs e)
{
using (Bitmap bmp = new Bitmap(pictureBox1.ClientSize.Width,
pictureBox1.ClientSize.Height))
{
using (Graphics g = Graphics.FromImage(bmp))
{
Image yourImage = pictureBox1.Image;
Bitmap yourBitmap = new Bitmap(yourImage);
g.DrawImage(yourBitmap,
new Rectangle(0, 0, bmp.Width, bmp.Height),
new Rectangle(0, 0, yourImage.Width, yourImage.Height),
GraphicsUnit.Pixel);
}
bmp.Save(@"d:\yourfile.png", ImageFormat.Png);
}
}
【问题讨论】: