Groups | Blog | Home
all groups > dotnet drawing api > september 2005 >

dotnet drawing api : Cropping resized images


David
9/27/2005 12:00:00 AM
Hi all,

I have the following code...

Bitmap bitmap = new Bitmap((int)new_width, (int)new_height,
m_src_image.PixelFormat);

m_graphics = Graphics.FromImage(bitmap);

m_graphics.SmoothingMode =
System.Drawing.Drawing2D.SmoothingMode.HighQuality;

m_graphics.InterpolationMode =
System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;


m_graphics.DrawImage(m_src_image, 0, 0, bitmap.Width, bitmap.Height);

m_dst_image = bitmap;

When I resize an image (large to small) I get a distinct edge on the top and
left of the image, about 2 pixels wide. This is a distraction, so I have
thought about cropping it to drop the edge. I am not sure though how to do
it.

The way I think is to make the original image 2 pixels larger in both
directions, then crop from top + 2 and left + 2. I am only guessing but not
sure here, the line with...
m_graphics.DrawImage(m_src_image, 0, 0, bitmap.Width, bitmap.Height);
if I make the parameters
(m_src_image, 2, 2, bitmap.Width + 2, bitmap.Height + 2)
would this crop the image, or would it move the canvas?

If it doesn't crop, can you suggest an alternative?

Best regards,
Dave Colliver.
http://www.DoncasterFOCUS.com
~~
http://www.FOCUSPortals.com - Local franchises available

David
9/27/2005 12:00:00 AM
Hi,

Thanks. I am not sure how to implement your suggestion, but I expanded on
what I thought.

I came up with
m_graphics.DrawImage(m_src_image, -2, -2, bitmap.Width+2, bitmap.Height+2);

This seems to work though I don't know if I am missing something. The image
seems the same size as requested, and I have tried the fix on an image with
white edges and one with dark edges. I can't see anything missing.

Best regards,
Dave Colliver.
http://www.AshfieldFOCUS.com
~~
http://www.FOCUSPortals.com - Local franchises available

[quoted text, click to view]

Michael Phillips, Jr.
9/27/2005 10:20:39 AM
You could define a Rectangle for that portion of the bitmap
that you wish to crop and use the Bitmap Clone method
to create a cropped bitmap.


[quoted text, click to view]

Michael Phillips, Jr.
9/27/2005 10:53:38 AM
Using Clone is easy. Here is an example:

Bitmap origBitmap = new Bitmap("Test.bmp");
// Clone a portion of the Bitmap, for example don't
// copy the 2 pixel border of the original bitmap
RectangleF cloneRect = new RectangleF( 2, 2, origBitmap.Width - 2,
origBitmap.Height - 2);
// Now create a copy of the Bitmap without the border
Bitmap cloneBitmap = origBitmap.Clone(cloneRect, origBitmap.PixelFormat);
// Draw the borderless bitmap
m_graphics.DrawImage(cloneBitmap, 0, 0, cloneBitmap.Width,
cloneBitmap.Height);

I hope this helps!


[quoted text, click to view]

AddThis Social Bookmark Button