HighTechTalks DotNet Forums  

Losing pixels from edge when resizing and using interpolation

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


Discuss Losing pixels from edge when resizing and using interpolation in the Dotnet Framework (Drawing) forum.



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

Default Losing pixels from edge when resizing and using interpolation - 11-22-2006 , 09:59 AM






Hi

I have now spent the better part of two days trying to solve this problem,
and really hope somebody here can help me.

When i resize an image, and use antialias, I lose important image
information.

This is the code:

[code]
Dim scale_factor As Single = 0.25 'scale to 1/4 size
Dim bm_source As New Bitmap(New System.Drawing.Bitmap(TextBox1.Text))
Dim bm_dest As New Bitmap(CInt(bm_source.Width * scale_factor),
CInt(bm_source.Height * scale_factor))
Dim gr_dest As Graphics = Graphics.FromImage(bm_dest)
'gr_dest.InterpolationMode =
System.Drawing.Drawing2D.InterpolationMode.HighQua lityBicubic
gr_dest.DrawImage(bm_source, 0, 0, bm_dest.Width, bm_dest.Height)
bm_dest.Save("C:\Temp\ResizeProbs\Out.jpg",
System.Drawing.Imaging.ImageFormat.Jpeg)
bm_dest = Nothing
[end code]

This is the source-image i created for testing purposes:

http://agilator.dk/newsimages/source.jpg

Notice the 4 pixel border all the way around the image.

When I leave the interpolation out (the line commented out) the image comes
out ugly, but with the right size borders:

http://agilator.dk/newsimages/Out_NoAntialias.jpg

When I enable interpolationmode and set it to HighQualityBicubic (or other
quality modes, I tried them all), the border is still displaed ok at the top
and on the left side. At the bottom and the right side, however, the border
has a different size. Check out the image:

http://agilator.dk/newsimages/Out_Antialias.jpg


This seems like a tiny detail, but when resizing images with borders to
thumbnails, it looks like there is only a border at the top and on the left
side - so it basicly looks wrong.

How can I solve this? Any help will be greatly appreciated!

Thanks in advance

- Klaus Jensen, MCAD



Reply With Quote
  #2  
Old   
Bob Powell [MVP]
 
Posts: n/a

Default Re: Losing pixels from edge when resizing and using interpolation - 11-22-2006 , 02:25 PM






Unfortunately this is an unavoidable effect of the antialiasing system.
Because pixels are resized into an area which is effectively _between_
destination pixels or into an area smaller than one destination pixel the
interpolation system tries to give an average value which is the best match
for the output. Sometimes it just doesn't work.

In many instances I prefer the HighQualityBilinear antialiasing because it
works in horizontal and vertical resizing but doesn't generate the diagonal
average. This can be better for viewing because an instinctive mechanism in
the visual cortex processes horizontal and vertical lines for us so that we
don't walk into trees or off of cliffs.

--
Bob Powell [MVP]
Visual C#, System.Drawing

Ramuseco Limited .NET consulting
http://www.ramuseco.com

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.



"Klaus Jensen" <spammers (AT) burninhell (DOT) com> wrote

Quote:
Hi

I have now spent the better part of two days trying to solve this problem,
and really hope somebody here can help me.

When i resize an image, and use antialias, I lose important image
information.

This is the code:

[code]
Dim scale_factor As Single = 0.25 'scale to 1/4 size
Dim bm_source As New Bitmap(New System.Drawing.Bitmap(TextBox1.Text))
Dim bm_dest As New Bitmap(CInt(bm_source.Width * scale_factor),
CInt(bm_source.Height * scale_factor))
Dim gr_dest As Graphics = Graphics.FromImage(bm_dest)
'gr_dest.InterpolationMode =
System.Drawing.Drawing2D.InterpolationMode.HighQua lityBicubic
gr_dest.DrawImage(bm_source, 0, 0, bm_dest.Width, bm_dest.Height)
bm_dest.Save("C:\Temp\ResizeProbs\Out.jpg",
System.Drawing.Imaging.ImageFormat.Jpeg)
bm_dest = Nothing
[end code]

This is the source-image i created for testing purposes:

http://agilator.dk/newsimages/source.jpg

Notice the 4 pixel border all the way around the image.

When I leave the interpolation out (the line commented out) the image
comes out ugly, but with the right size borders:

http://agilator.dk/newsimages/Out_NoAntialias.jpg

When I enable interpolationmode and set it to HighQualityBicubic (or other
quality modes, I tried them all), the border is still displaed ok at the
top and on the left side. At the bottom and the right side, however, the
border has a different size. Check out the image:

http://agilator.dk/newsimages/Out_Antialias.jpg


This seems like a tiny detail, but when resizing images with borders to
thumbnails, it looks like there is only a border at the top and on the
left side - so it basicly looks wrong.

How can I solve this? Any help will be greatly appreciated!

Thanks in advance

- Klaus Jensen, MCAD




Reply With Quote
  #3  
Old   
Klaus Jensen
 
Posts: n/a

Default Re: Losing pixels from edge when resizing and using interpolation - 11-25-2006 , 06:57 AM



"Bob Powell [MVP]" <bob (AT) _spamkiller_ (DOT) bobpowell.net> wrote


Thanks a lot for your answer, Bob. I will try HighQualityBilinear again and
try explaining it to the customer and maybe they'll accept it. It is either
that or no interpolation, I guess...

And thanks for your great website btw, I have used your FAQ many, many
times!

- Klaus



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 - 2008, Jelsoft Enterprises Ltd.