Cделала простую программу по типу поинта, возникла проблема в сохранении изображения с панели, окно сохранения выскакивает, но в итоге ничего не сохраняет, в чем может быть проблема? Так выглядит программа:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
g = pnl_Draw.CreateGraphics();
}
bool startPaint = false;
Graphics g;
int? initX = null;
int? initY = null;
bool drawSquare = false;
bool drawRectangle = false;
bool drawCircle = false;
private void pnl_Draw_MouseDown(object sender, MouseEventArgs e)
{
startPaint = true;
if (drawSquare)
{
// use Solid Brush for filling the graphic shapes
SolidBrush sb = new SolidBrush(btn_PenColor.BackColor);
// setting the width and height same for creating square
// getting the width and higth value from Textbox
g.FillRectangle(sb, e.X, e.Y, int.Parse(txt_ShapeSize.Text), int.Parse(txt_ShapeSize.Text));
// setting startPint and drawSquare value to false for creating one graphic on one click
startPaint = false;
drawSquare = false;
}
if (drawRectangle)
{
SolidBrush sb = new SolidBrush(btn_PenColor.BackColor);
// setting the width twic of the height
g.FillRectangle(sb, e.X, e.Y, 2 * int.Parse(txt_ShapeSize.Text), int.Parse(txt_ShapeSize.Text));
startPaint = false;
drawRectangle = false;
}
if (drawCircle)
{
SolidBrush sb = new SolidBrush(btn_PenColor.BackColor);
g.FillEllipse(sb, e.X, e.Y, int.Parse(txt_ShapeSize.Text), int.Parse(txt_ShapeSize.Text));
startPaint = false;
drawCircle = false;
}
}
private void btn_kwadrat_Click(object sender, EventArgs e)
{
drawSquare = true;
}
private void btn_kolo_Click(object sender, EventArgs e)
{
drawCircle = true;
}
private void btn_prostokond_Click(object sender, EventArgs e)
{
drawRectangle = true;
}
private void pnl_Draw_MouseMove(object sender, MouseEventArgs e)
{
if (startPaint)
{
Pen p = new Pen(btn_PenColor.BackColor, float.Parse(cmb_PenSize.Text));
g.DrawLine(p, new Point(initX ?? e.X, initY ?? e.Y), new Point(e.X, e.Y));
initX = e.X;
initY = e.Y;
}
}
private void btn_PenColor_Click(object sender, EventArgs e)
{
ColorDialog color = new ColorDialog();
if (color.ShowDialog() == DialogResult.OK)
{
btn_PenColor.BackColor = color.Color;
}
}
private void pnl_Draw_MouseUp(object sender, MouseEventArgs e)
{
startPaint = false;
initX = null;
initY = null;
}
private void btn_zapiśz_Click(object sender, EventArgs e)
{
SaveFileDialog saveFileDialog1 = new SaveFileDialog();
saveFileDialog1.Filter = "Jpeg Image|*.jpg| Bitmap Image|*bmp|Gif Image|*gif";
saveFileDialog1.Title = "Save an Image File";
saveFileDialog1.ShowDialog();
if (saveFileDialog1.FileName != "")
{
MemoryStream ms = new MemoryStream();
Bitmap bmp = new Bitmap(pnl_Draw.Width, pnl_Draw.Height);
pnl_Draw.DrawToBitmap(bmp, new Rectangle(0, 0, pnl_Draw.Width, pnl_Draw.Height));
bmp.Save("panel.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
switch (saveFileDialog1.FilterIndex)
{
case 1:
bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
break;
case 2:
bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
break;
case 3:
bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
break;
}
ms.Close();
}
}
private void btn_odczytaj_Click(object sender, EventArgs e)
{
OpenFileDialog fileDialog = new OpenFileDialog();
if (fileDialog.ShowDialog() == DialogResult.OK)
{
this.pnl_Draw.BackgroundImage = Image.FromFile(fileDialog.FileName);
}
}
private void zamknij_Click(object sender, EventArgs e)
{
if (MessageBox.Show("Do you want to Exit?", "Exit", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes)
{
Application.Exit();
}
}
}
Фрагмент кода с сохранением:
private void btn_zapiśz_Click(object sender, EventArgs e) {
SaveFileDialog saveFileDialog1 = new SaveFileDialog();
saveFileDialog1.Filter = "Jpeg Image|*.jpg| Bitmap Image|*bmp|Gif Image|*gif";
saveFileDialog1.Title = "Save an Image File";
saveFileDialog1.ShowDialog();
if (saveFileDialog1.FileName != "")
{
MemoryStream ms = new MemoryStream();
Bitmap bmp = new Bitmap(pnl_Draw.Width, pnl_Draw.Height);
pnl_Draw.DrawToBitmap(bmp, new Rectangle(0, 0, pnl_Draw.Width, pnl_Draw.Height));
bmp.Save("panel.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
switch (saveFileDialog1.FilterIndex)
{
case 1:
bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
break;
case 2:
bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
break;
case 3:
bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
break;
}
ms.Close();
}