How about checking if file name has .jpg
extension before saving it?
You can also change saveDialog
to only allow user selecting .jpg
images.
private void btnSave_Click(object sender, EventArgs e)
{
saveDialog.FileName = txtModelName.Text;
saveDialog.DefaultExt = "jpg";
saveDialog.Filter = "JPG images (*.jpg)|*.jpg";
if (saveDialog.ShowDialog() == DialogResult.OK)
{
Bitmap bmp = new Bitmap(pnlDraw.Width, pnlDraw.Height);
pnlDraw.DrawToBitmap(bmp, new Rectangle(0, 0,
pnlDraw.Width, pnlDraw.Height));
var fileName = saveDialog.FileName;
if(!System.IO.Path.HasExtension(fileName) || System.IO.Path.GetExtension(fileName) != "jpg")
fileName = fileName + ".jpg";
bmp.Save(fileName, System.Drawing.Imaging.ImageFormat.Jpeg);
}
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…