我正在尝试保存在图片框中固定的图像,绘制所需的图像,但是当保存时,线条未显示,如何保存?
我需要将此图像保存在我的本地文件夹中,然后通过抽奖调用该图像
public Form1()
{
InitializeComponent();
g = ppaint.CreateGraphics();
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
pen = new Pen(Color.Black, 5);
pen.StartCap = pen.EndCap = System.Drawing.Drawing2D.LineCap.Round;
}
Graphics g;
int x = -1;
int y = -1;
bool mover = false;
bool borrar = false;
Pen pen;
Bitmap btm;
private void colorPickerBox_Click(object sender, EventArgs e)
{
// Set the pen color based on selected pictureBox in the color picker
PictureBox p = (PictureBox)sender;
pen.Color = p.BackColor;
btm = new Bitmap(ppaint.Width, ppaint.Height);
Rectangle rect = new Rectangle(0, 0, ppaint.Width, ppaint.Height);
ppaint.DrawToBitmap(btm, rect);
}
private void ppaint_MouseDown(object sender, MouseEventArgs e)
{
mover = true;
x = e.X;
y = e.Y;
}
private void ppaint_MouseMove(object sender, MouseEventArgs e)
{
if (mover && x != -1 && y != -1)
{
g.DrawLine(pen, new Point(x, y), e.Location);
x = e.X;
y = e.Y;
}
}
private void ppaint_MouseUp(object sender, MouseEventArgs e)
{
mover = false;
x = -1;
y = -1;
}
private void btnsave_Click(object sender, EventArgs e)
{
btm.Save($@"C:\Odontogramas\odn.jpep", ImageFormat.Jpeg);
}
问题是您直接吸引到PictureBox ppaint
,而不是Bitmap btm
;当通过DrawToBitmap()
调用将原始图像保存到位图中时(我猜它是PictureBox ppaint
控件的背景图像),其他内容均未绘制。
有2个可能的解决方法:
而不是通过PictureBox ppaint
控件创建图形对象,而是使用Graphics.FromImage(btm)
调用来获取绑定到位图的Graphics
对象。这是使用GDI +(WinForms图形API)的首选方法。但是,每次绘制时都必须更新BackgroundImage
属性,以用新绘制的图像替换原始图像。
在您的情况下,一个更简单的解决方案是将DrawToBitmap()
调用移到您的保存代码中,就在您调用btm.Save()
之前-这应该同时使用背景图像和绘制的图形(线条)来更新位图。 PictureBox ppaint
控件。