PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, July 26, 2022

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

 July 26, 2022     c#, crop     No comments   

Issue

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)?


Solution

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.



Answered By - JonasH
Answer Checked By - Cary Denson (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing