Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
How do I crop the bitmap and save it in its original quality? Height of bitmap should be 592 and width 699.
varas.ptval is orignal bitmap.

What I have tried:

C#
Rectangle rectangle = new Rectangle();
rectangle.Height = 100;
rectangle.Width = 100;
rectangle.X = 100;
rectangle.Y = 100;

Bitmap temp = new Bitmap(varas.ptval, varas.ptval.Width,varas.ptval.Height);
temp = varas.ptval;
var croppedImagem = cropBitmap(temp, rectangle);
croppedImagem.Save(paths + "\\" + "SPR_" + i + ".jpeg", ImageFormat.Jpeg);
                           

 public static Bitmap CropBitmap(Bitmap bitmap, int x, int y, int w, int h)
        {
            Rectangle rect = new Rectangle(x, y, w, h);
            Bitmap cropped = bitmap.Clone(rect, bitmap.PixelFormat);
            return cropped;
        }
Posted
Updated 20-Dec-22 12:35pm
Comments
Richard Deeming 20-Dec-22 4:24am    
REPOST
You already posted this, and accepted a solution:
How to crop bitmap and save with original clarity of image ?[^]

The answer hasn't changed in the last 24 hours!

1 solution

Bitmap is not a "lossy format" so cropping it doesn't change the quality, it just changes the area that the new Bitmap contains.

JPEG (or jpg) is a lossy format though: if you store images in .JPG or .JPEG files then each time you save it you degrade the quality. Indeed, you can destroy an image pretty thoroughly, just by loading and saving it repeatedly as a JPG.

If you want to preserve the quality, use a non-lossy format such as BMP, PNG, or TIFF rather than JPEG.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900