HighTechTalks DotNet Forums  

Cropping resized images

Dotnet Framework (Drawing) microsoft.public.dotnet.framework.drawing


Discuss Cropping resized images in the Dotnet Framework (Drawing) forum.



Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old   
David
 
Posts: n/a

Default Cropping resized images - 09-27-2005 , 08:51 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.HighQua lityBicubic;


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



Reply With Quote
  #2  
Old   
Michael Phillips, Jr.
 
Posts: n/a

Default Re: Cropping resized images - 09-27-2005 , 09:20 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.


"David" <david.colliver.NEWS (AT) revilloc (DOT) REMOVETHIS.com> wrote

Quote:
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.HighQua lityBicubic;


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




Reply With Quote
  #3  
Old   
David
 
Posts: n/a

Default Re: Cropping resized images - 09-27-2005 , 09:34 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

"Michael Phillips, Jr." <mphillips53 (AT) nospam (DOT) jun0.c0m> wrote

Quote:
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.


"David" <david.colliver.NEWS (AT) revilloc (DOT) REMOVETHIS.com> wrote in message
news:ud3Cl02wFHA.460 (AT) TK2MSFTNGP15 (DOT) phx.gbl...
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.HighQua lityBicubic;


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






Reply With Quote
  #4  
Old   
Michael Phillips, Jr.
 
Posts: n/a

Default Re: Cropping resized images - 09-27-2005 , 09:53 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!


"David" <david.colliver.NEWS (AT) revilloc (DOT) REMOVETHIS.com> wrote

Quote:
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

"Michael Phillips, Jr." <mphillips53 (AT) nospam (DOT) jun0.c0m> wrote in message
news:uyv1h62wFHA.460 (AT) TK2MSFTNGP15 (DOT) phx.gbl...
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.


"David" <david.colliver.NEWS (AT) revilloc (DOT) REMOVETHIS.com> wrote in message
news:ud3Cl02wFHA.460 (AT) TK2MSFTNGP15 (DOT) phx.gbl...
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.HighQua lityBicubic;


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








Reply With Quote
Reply




Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.