How do I crop the bitmap and save it in its original quality? Height of bitmap should be 592 and width 699
Loading
How do I crop the bitmap and save it in its original quality? Height of bitmap should be 592 and width 699
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
NitinRank #140 · 16.1K reputationPosted Dec 20, 2022, 12:25 AM EST
Hi Ridhdi vaghani
check this URL- https://www.codeproject.com/Questions/5349954/How-to-crop-bitmap-and-save-with-original-clarity
Deepak TewatiaRank #142 · 16K reputationPosted Dec 20, 2022, 3:49 AM EST
Hi Ridhdhi,
Based on your peoblem statement, use the below steps:-
try to use below code to solve your problem:
// Load the original bitmap Bitmap original = new Bitmap("original.bmp"); // Create a rectangle representing the area to be cropped Rectangle cropRect = new Rectangle(0, 0, 699, 592); // Create a new bitmap the same size as the crop rectangle Bitmap cropped = new Bitmap(cropRect.Width, cropRect.Height); // Create a graphics object to draw the cropped image using (Graphics g = Graphics.FromImage(cropped)) { // Draw the cropped image g.DrawImage(original, new Rectangle(0, 0, cropped.Width, cropped.Height), cropRect, GraphicsUnit.Pixel); } // Save the cropped image to a file cropped.Save("cropped.bmp", ImageFormat.Bmp);It will load the original bitmap then create a new bitmap that is the same size as the crop rectangle then draw the cropped image onto the new bitmap and then save the new bitmap to a file. The resulting image will be the same quality as the original because it is simply a copy of a portion of the original image.
Thanks
Naimish MakwanaRank #324 · 7.7K reputationPosted Feb 9, 2023, 3:16 AM EST
Hello Riddhi,
Please refer below link:
https://learn.microsoft.com/en-us/dotnet/desktop/winforms/advanced/how-to-set-jpeg-compression-level?view=netframeworkdesktop-4.8
Thanks
Naimish Makwana
Kip Hackman61 reputationPosted Feb 8, 2023, 12:10 PM EST
You can use LEADTOOLS Imaging Pro SDK technology in your application.
https://www.leadtools.com/sdk/products/imaging-pro
You can leverage the CropCommand class, which will allow you to take the points you have and crop your image. When you use the CropCommand, the quality(DPI) of the image will not change. See the sample code below:
string srcFileName = @"FILE PATH TO INPUT IMAGE"; string destFileName = @"FILE PATH TO OUTPUT CROPPED IMAGE"; using (RasterCodecs _codecs = new RasterCodecs()) using (RasterImage _image = _codecs.Load(srcFileName)) { CropCommand _cmd = new CropCommand(); _cmd.Rectangle = new LeadRect( 0, 0, 99, 592); _cmd.Run(image); _codecs.Save(_image, destFileName, RasterImageFormat.Bmp, 0); }