C# 在winform使用Graphics画图保存问题,求大神帮忙
创建了一个MDI窗体。在子窗体上创建了一个pictureBox.定义了一个bitmapsbmp。privatevoidForm1_Load(objectsender,Ev...
创建了一个MDI窗体。在子窗体上创建了一个pictureBox.定义了一个bitmap sbmp。 private void Form1_Load(object sender, EventArgs e)
{ //创建一个透明的位图并绘制到屏幕
Sbmp = new Bitmap(this.pictureBox1.Width, this.pictureBox1.Height);
Graphics newGrashics = Graphics.FromImage(Sbmp);
newGrashics.FillRectangle(new SolidBrush(this.BackColor), 0, 0, this.pictureBox1.Width, this.pictureBox1.Height);
Color backColor = Sbmp.GetPixel(1, 1);
Sbmp.MakeTransparent(backColor);
newGrashics.DrawImage(Sbmp, Sbmp.Width, 0, Sbmp.Width, Sbmp.Height);
newGrashics.Dispose();
dColor = Color.Black;
dWidthThin = 1;
dt = new Draw(this.pictureBox1.CreateGraphics(), Sbmp, dColor, dWidthThin);
}dt为画图类的构造函数。里面有各种画图的方法。如何在父窗体保存sbmp时,保存绘制了的图。
private void 保存ToolStripMenuItem_Click(object sender, EventArgs e)
{
if (CurrentFrm.Sbmp == null) return;
SaveFileDialog saveDlg = new SaveFileDialog();
saveDlg.Title = "保存为";
saveDlg.OverwritePrompt = true;
saveDlg.Filter = "BMP Files(*.bmp)|*.bmp|JPG Files(*.jpg;*.jpeg)|*.jpg;*.jpeg|All Files(*.*)|*.*";
saveDlg.InitialDirectory = Application.StartupPath;//保存的目录在启动项下
saveDlg.ShowHelp = true;
if (saveDlg.ShowDialog() == DialogResult.OK)
{
string fileName = saveDlg.FileName;
string strFilExtn = fileName.Remove(0, fileName.Length - 3);
CurrentFrm.pictureBox1.Image = CurrentFrm.Sbmp;
Graphics newGraphics = Graphics.FromImage(CurrentFrm.pictureBox1.Image);
CurrentFrm.pictureBox1.Image.Save(saveDlg.FileName);
//CurrentFrm.Sbmp.Save(saveDlg.FileName);
MessageBox.Show("保存成功!", "提示");
saveDlg.Dispose();
}
}
}目前保存的是空的sbmp。不管我绘制没绘制,都只是保存下来创建的sbmp。 展开
{ //创建一个透明的位图并绘制到屏幕
Sbmp = new Bitmap(this.pictureBox1.Width, this.pictureBox1.Height);
Graphics newGrashics = Graphics.FromImage(Sbmp);
newGrashics.FillRectangle(new SolidBrush(this.BackColor), 0, 0, this.pictureBox1.Width, this.pictureBox1.Height);
Color backColor = Sbmp.GetPixel(1, 1);
Sbmp.MakeTransparent(backColor);
newGrashics.DrawImage(Sbmp, Sbmp.Width, 0, Sbmp.Width, Sbmp.Height);
newGrashics.Dispose();
dColor = Color.Black;
dWidthThin = 1;
dt = new Draw(this.pictureBox1.CreateGraphics(), Sbmp, dColor, dWidthThin);
}dt为画图类的构造函数。里面有各种画图的方法。如何在父窗体保存sbmp时,保存绘制了的图。
private void 保存ToolStripMenuItem_Click(object sender, EventArgs e)
{
if (CurrentFrm.Sbmp == null) return;
SaveFileDialog saveDlg = new SaveFileDialog();
saveDlg.Title = "保存为";
saveDlg.OverwritePrompt = true;
saveDlg.Filter = "BMP Files(*.bmp)|*.bmp|JPG Files(*.jpg;*.jpeg)|*.jpg;*.jpeg|All Files(*.*)|*.*";
saveDlg.InitialDirectory = Application.StartupPath;//保存的目录在启动项下
saveDlg.ShowHelp = true;
if (saveDlg.ShowDialog() == DialogResult.OK)
{
string fileName = saveDlg.FileName;
string strFilExtn = fileName.Remove(0, fileName.Length - 3);
CurrentFrm.pictureBox1.Image = CurrentFrm.Sbmp;
Graphics newGraphics = Graphics.FromImage(CurrentFrm.pictureBox1.Image);
CurrentFrm.pictureBox1.Image.Save(saveDlg.FileName);
//CurrentFrm.Sbmp.Save(saveDlg.FileName);
MessageBox.Show("保存成功!", "提示");
saveDlg.Dispose();
}
}
}目前保存的是空的sbmp。不管我绘制没绘制,都只是保存下来创建的sbmp。 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询