LanternCode
LanternCode

Reputation: 23

Image does not convert to Bitmap

I am currently experiencing an issue where an image cannot be converted to Bitmap.

This is the code in question:

private void btnPicture_Click(object sender, EventArgs e)
        {
            OpenFileDialog opFile = new OpenFileDialog();
            opFile.Title = "Choose a picture!";
            opFile.Filter = "jpg files (*.jpg)|*.jpg|All files (*.*)|*.*";

            string appPath = Path.GetDirectoryName(Application.ExecutablePath);
            if (Directory.Exists(appPath) == false)
            {
                Directory.CreateDirectory(appPath);
            }

            if (opFile.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    Character.imageName = opFile.SafeFileName;
                    Stream stream = opFile.OpenFile(); //<------ This is the line in question
                    Bitmap toResize = new Bitmap(stream);

                    Image characterImage = ResizeImage(toResize, 200, 306);

                    imgCharacter.Image = characterImage;
                    characterImage.Save("Images/" + Character.imageName);
                }
                catch (Exception exp)
                {
                    MessageBox.Show("Please select a picture, exception code: " + exp.Message);
                }
            }
            else
            {
                opFile.Dispose();
            }
        }

Now basically this code works with every single image that I have tried, except 1. A single picture that my friends tried to upload will crash with the following error: Parameter is not valid.

What I have tried:

Link to the image: https://cdn.discordapp.com/attachments/413040202307731466/713873776143499294/babar1.jpg

I tried to find help online but could not find anything relevant so here I am, if there is anything particularly special to this picture (or a group of pictures) that I do not know about, please forgive, I am not a graphics designer in any way, shape or form.

Have a nice day.

Upvotes: 1

Views: 1297

Answers (1)

DSander
DSander

Reputation: 1124

I looked at the image you provided named "barbar1.jpg" in a hex editor and can see RIFF WEBPVP8 markers at the beginning. This means the image is not a JPG file it is a WEBP file.

https://en.wikipedia.org/wiki/WebP

The error you are getting is when initializing the new Bitmap from a stream. Bitmap class only supports BMP, GIF, EXIF, JPG, PNG and TIFF.

Microsoft Paint supports WEBP which is why you are able to open it and view it. Microsoft and other image viewers ignore the file extension and parse the image based on the format of the file data itself. This is why you are able to rename the file to PNG, JPG, BMP, etc and it will still be readable by Paint.

Upvotes: 3

Related Questions