Click here to Skip to main content
15,905,877 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In my App, I load an image. Since this image contains some useless information on its top and bottom area, I want to re-draw the image and only take its center part of the original (like Paint's Select Tool). Is it possible? Thanks.

What I have tried:

How to select part of an image dynamically?
Posted
Updated 6-Feb-19 8:01am

1 solution

Create a new Bitmap the correct size, and use DrawImage to draw only the required area into it: Graphics.DrawImage Method (System.Drawing) | Microsoft Docs[^] The first overload should do it: Graphics.DrawImage Method (System.Drawing) | Microsoft Docs[^]
 
Share this answer
 
Comments
s yu 7-Feb-19 11:18am    
Thanks for your response. Per your answer, I did the following

SaveImageFile(image, "Temp");
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(image);
RectangleF rect = new RectangleF(0,25,100,100);
g.DrawImage(image, 25F, 25F, rect, GraphicsUnit.Point);
g.DrawImage(image, rect);
Bitmap bmp = new Bitmap(100, 100, g);
SaveImageFile(bmp, "Temp_");

I saved both images. The 1st one is fine, but the 2nd one is nothing but displays a black box. What wrong in code? Thanks if you can help.
OriginalGriff 7-Feb-19 14:05pm    
"Create a new Bitmap the correct size" - did you do that? No.
"use DrawImage to draw only the required area into it" - did you do that? No, you drew onto the image you are drawing from ... twice.
Then you create a new bitmap, draw nothing on it, and save it ...

Please, read what I said, and try to think before you rush into code. :laugh:

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