8

The problem is that the file is not saving as JPEG. Just a normal file.

This is my code so far:

private void btnSave_Click(object sender, EventArgs e) { saveDialog.FileName = txtModelName.Text; if (saveDialog.ShowDialog() == DialogResult.OK) { Bitmap bmp = new Bitmap(pnlDraw.Width, pnlDraw.Height); pnlDraw.DrawToBitmap(bmp, new Rectangle(0, 0, pnlDraw.Width, pnlDraw.Height)); bmp.Save(saveDialog.FileName, System.Drawing.Imaging.ImageFormat.Jpeg); } } 
6
  • What does "normal file" mean? Commented Jan 19, 2014 at 14:31
  • It looks correct to me. I suspect "normal file" just means that the file doesn't have the right extension. Commented Jan 19, 2014 at 14:32
  • I think your problem is simply the extension. What happens if you open the file "image1" with an imageviewer? I mean, is it an image that is simply missing an extension? Commented Jan 19, 2014 at 14:32
  • In file properties it just says "File Type : File". Commented Jan 19, 2014 at 14:33
  • Actually it doesn't open anymore it tells me to choose a program to open it with Commented Jan 19, 2014 at 14:33

2 Answers 2

17

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); } } 
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks it works but when I save the image why doesn't it show JPEG Save File Type:?
@GameOver I've updated my answer with 2 lines which should make the trick.
1
int width; int height; int stride; int x; int y; string filename; Bitmap bitmap = new Bitmap(width,height,stride,PixelFormat.Format32bppArgb,new IntPtr()); //then set the pixel; bitmap.SetPixel(x,y,Colors.Black); bitmap.Save(filename); 

3 Comments

Hey, welcome to the site and thank you for your answer. Maybe you could edit this post to explain how this fixes the question and how to use it? That way people who come through in the future can understand what's happening better.
can you please add a little bit of clarification
Somebody incorrectly flagged your answer as low-quality. You should add some accompanying text to explain how your answer works to prevent further flagging and/or downvotes. A code-only answer is not low-quality. Does it attempt to answer the question? If not, flag as 'not an answer' or recommend deletion (if in the review queue). b) Is it technically incorrect? Downvote or comment. From review.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.