How to overwrite original image with cropped version of this image in c# desktop application

0

I am building c# contact manager desktop application where you can choose an avatar image from the file and later crop it. The original image is properly displayed after cropping, replacing the original one. However, later, upon attempting to register the account the image appears to be Null with such an error being displayed:

System.ArgumentNullException: „Value cannot be null. (Parameter 'encoder')”

The method for uploading an image to the register page:

        private void button_browse_Click(object sender, EventArgs e)
        {
            // select and display image in the picturebox
            OpenFileDialog opf = new OpenFileDialog();
            opf.Filter = "Select Image(*.jpg;*.png;*.gif)|*.jpg;*.png;*.gif";
            

            if(opf.ShowDialog() == DialogResult.OK)
            {
                pictureBoxProfileImage.Image = Image.FromFile(opf.FileName);
                setImage(pictureBoxProfileImage.Image);
            }
        }

The method for cropping the image:

private void button_Select_Cropped_Area_Click(object sender, EventArgs e)
        {
            Cursor = Cursors.Default;

            Bitmap bitmap2 = new Bitmap(pictureBoxProfileImage.Width, pictureBoxProfileImage.Height);
            pictureBoxProfileImage.DrawToBitmap(bitmap2, pictureBoxProfileImage.ClientRectangle);

            Bitmap croppedImage = new Bitmap(rectW, rectH);
            for (int x = 0; x < rectW; x++)
            {
                for (int y = 0; y < rectH; y++)
                {
                    Color pxlColor = bitmap2.GetPixel(cropX + x, cropY + y);
                    croppedImage.SetPixel(x, y, pxlColor);
                }
            }
            pictureBoxProfileImage.Image.Dispose();
            pictureBoxProfileImage.Image = (Image)croppedImage;
            pictureBoxProfileImage.SizeMode = PictureBoxSizeMode.StretchImage;
        }

Here's the line where the error occurs when saving the image:

MemoryStream picture = new MemoryStream();
pictureBoxProfileImage.Image.Save(picture, pictureBoxProfileImage.Image.RawFormat);

It is worth mentioning that the register works properly if the original image is being passed. Should I replace them somehow (overwrite the original one with the cropped one)?

c# crop
2021-11-15 11:20:12
1

1

My guess is that the failure occurs due to pictureBoxProfileImage.Image.RawFormat. You just replaced pictureBoxProfileImage.Image, but what rawFormat does a new Bitmap(...) have? While I cannot find any documentation about it, I would guess that it does not have any valid rawFormat at all.

So I would try to either replace it with something like ImageFormat.Png, or save the rawFormat from the original bitmap to use when saving.

2021-11-15 13:51:04

In other languages

This page is in other languages

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
中文
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................