我将其编码为图片框上的划痕图片。
bool draw = false;
int pX = -1;
int pY = -1;
Bitmap drawing;
public Form1()
{
drawing = new Bitmap(transformedImage.Width, transformedImage.Height, transformedImage.CreateGraphics());
Graphics.FromImage(drawing).Clear(Color.Transparent);
}
private void transformedImage_MouseMove(object sender, MouseEventArgs e)
{
if (draw)
{
int penWidth = Convert.ToInt32(penWidthValue.Value);
if(blackCheck.Checked == true) ///black pen
{
Graphics panel = Graphics.FromImage(drawing);
Pen pen = new Pen(Color.Black, penWidth);
pen.EndCap = LineCap.Round;
pen.StartCap = LineCap.Round;
panel.DrawLine(pen, pX, pY, e.X, e.Y);
transformedImage.CreateGraphics().DrawImageUnscaled(drawing, new Point(0, 0));
}
else if(redCheck.Checked == true) /// red pen
{
Graphics panel = Graphics.FromImage(drawing);
Pen pen = new Pen(Color.Red, penWidth);
pen.EndCap = LineCap.Round;
pen.StartCap = LineCap.Round;
panel.DrawLine(pen, pX, pY, e.X, e.Y);
transformedImage.CreateGraphics().DrawImageUnscaled(drawing, new Point(0, 0));
}
else if(yellowCheck.Checked == true) /// yellow
{
Graphics panel = Graphics.FromImage(drawing);
Pen pen = new Pen(Color.Yellow, penWidth);
pen.EndCap = LineCap.Round;
pen.StartCap = LineCap.Round;
panel.DrawLine(pen, pX, pY, e.X, e.Y);
transformedImage.CreateGraphics().DrawImageUnscaled(drawing, new Point(0, 0));
}
else /// green
{
Graphics panel = Graphics.FromImage(drawing);
Pen pen = new Pen(Color.Green, penWidth);
pen.EndCap = LineCap.Round;
pen.StartCap = LineCap.Round;
panel.DrawLine(pen, pX, pY, e.X, e.Y);
transformedImage.CreateGraphics().DrawImageUnscaled(drawing, new Point(0, 0));
}
}
pX = e.X;
pY = e.Y;
}
private void transformedImage_MouseDown(object sender, MouseEventArgs e)
{
if (scratchCheck.Checked == true)
{
draw = true;
pX = e.X;
pY = e.Y;
}
}
private void transformedImage_MouseUp(object sender, MouseEventArgs e)
{
draw = false;
}
private void transformedImage_Paint(object sender, PaintEventArgs e)
{
e.Graphics.DrawImageUnscaled(drawing, new Point(0, 0));
}
但是当我用这个保存图像时:
SaveFileDialog sfd = new SaveFileDialog();
sfd.Title = "Select Save Location";
sfd.InitialDirectory = @"Save_Path";
sfd.AddExtension = true;
if (sfd.ShowDialog() == DialogResult.OK)
{
switch (Path.GetExtension(sfd.FileName).ToUpper())
{
case ".BMP":
bmp.Save(sfd.FileName, ImageFormat.Bmp);
break;
case ".gif":
bmp.Save(sfd.FileName, ImageFormat.Gif);
break;
case ".JPG":
bmp.Save(sfd.FileName, ImageFormat.Jpeg);
break;
case ".JPEG":
bmp.Save(sfd.FileName,ImageFormat.Jpeg);
break;
case ".PNG":
bmp.Save(sfd.FileName, ImageFormat.Png);
break;
case ".png":
bmp.Save(sfd.FileName, ImageFormat.Png);
break;
default:
break;
}
}
在图片框上划伤的图像没有刮伤。
如何保存图片以及划痕?非常感谢。
答案 0 :(得分:1)
两个常见错误:
使用CreateGraphics
不会创建持久性图形。所以:Alwas在Paint
的{{1}}事件中绘制了所有内容! PictureBox
只应收集 MouseMove
或List<Point>
中的积分。 所有绘图必须使用List<List<Point>>
对象在Paint
事件中发生!
如果您使用e.Graphics
创建新的位图,则表面上的绘图只会保存,该位图最多可组合三层:DrawToBitmap
, BackgroundImage
和所有持久性图纸。
有关绘图的示例,请参见here;有关使用Image
的示例,请参见here!
作为这种推荐的DrawToBitmap
绘图方式的替代方法,您也可以直接绘制它。这是Image
中实际执行的操作,但稍后您将忽略该图像并以非持久方式绘制它。
最后:MouseMove
例程正在保存您未向我们展示的Save
,因此我们只能断定它是您加载的原始bmp
..也许{{1}会帮助..
但代码有很多问题,你真的应该做一个完整的重写,从令人困惑的名字开始! (提示:不要将对象称为其他类型的名称,如Image
!!!)显然,drawing.Save(..)
或Graphics panel
类型的变量也有助于避免重复这些...