油漆程序不保存编辑图片

时间:2014-06-27 21:22:38

标签: c# image paint

正在开发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);
        }
    }

1 个答案:

答案 0 :(得分:1)

因为你在Bitmapped" yourBitmap"使用yourImage,尝试用yourBitmap.Save替换bmp.Save

    yourBitmap.Image.Save(@"d:\yourfile.png", ImageFormat.Png);