【发布时间】:2016-08-10 00:00:19
【问题描述】:
我尝试了很多方法,包括位图转换等。 这是我的代码。会喜欢它,有人会向我解释如何保存它以及为什么。谢谢!
public partial class Form1 : Form
{
Graphics G;
Pen myPen = new Pen(Color.Black);
Point sp = new Point(0, 0);
Point ep = new Point(0, 0);
int ctrl = 0;
public Form1()
{
InitializeComponent();
}
private void panel1_Paint(object sender, PaintEventArgs e)
{
}
private void panel1_MouseDown(object sender, MouseEventArgs e)
{
sp = e.Location;
if(e.Button == MouseButtons.Left)
{
ctrl = 1;
}
}
private void panel1_MouseUp(object sender, MouseEventArgs e)
{
ctrl = 0;
}
private void panel1_MouseMove(object sender, MouseEventArgs e)
{
if(ctrl == 1)
{
ep = e.Location;
G = panel1.CreateGraphics();
G.DrawLine(myPen, sp, ep);
}
sp = ep;
}
private void button1_Click(object sender, EventArgs e)
{
colorDialog1.ShowDialog();
myPen.Color = colorDialog1.Color;
colourBtn.BackColor = colorDialog1.Color;
}
private void clrBtn_Click(object sender, EventArgs e)
{
G.Clear(colorDialog2.Color);
}
private void button1_Click_1(object sender, EventArgs e)
{
colorDialog2.ShowDialog();
panel1.BackColor = colorDialog2.Color;
panel1Colourbtn.BackColor = colorDialog2.Color;
}
private void button1_Click_2(object sender, EventArgs e)
{
SaveFileDialog dlgSave = new SaveFileDialog();
dlgSave.Title = "Save Image";
dlgSave.Filter = "Bitmap Images (*.bmp)|*.bmp|All Files (*.*)|*.*";
if (dlgSave.ShowDialog(this) == DialogResult.OK)
{
using (Bitmap bmp = new Bitmap(panel1.Width, panel1.Height))
{
// how do i save my drawing using savefiledialog?
}
}
}
【问题讨论】:
-
Bitmap的Save()方法怎么样? -
你的问题不是保存,而是绘图!错误从这里开始:
G = panel1.CreateGraphics();- 一旦您仅从绘制事件中绘制,您就可以使用DrawToBitmap创建可保存的位图。现在你需要重写绘图代码! - See here for more cooments and links about the very same issues -
你的意思是我只需要在绘画事件中编写代码吗?
-
CreateGraphics 是屏幕上的临时绘图。如果您最小化表格,它很容易被删除。始终使用控件的绘制事件或从位图生成图形对象。
-
在工作时,接受的答案将无法执行,也不允许撤消。绘画程序应该允许在各种画布上绘画。当您有时间时,您可能想研究我提供的链接以获得更好和更可扩展的解决方案..
标签: c#