resizing image defect - strange border appears
Hi. I am implementing a code that takes an image of arbitrary size,
then crops it to proprotion and resizes the result to some set
thumbnail size.
The problem is, when the image is resized there's a visible darker
border along its edges (usually top and left), 1px wide, that is often
perceived as black border when the image is shown (even though the
border is actullay psrt of the image if you zoom in except that all
pixels are sufficiently darker).
Image are photographs and have no borders that could potentially be
exaggerated by decreasing size; I use bicubic interpolation and smooth
and high-quality everything.
Thus far, I wrote a code that makes an image 2px wider and higher than
required and then trims 1px from all sides.
Is there any better way to get rid of this bug border? Why does it
appear?
Date:Thu, 28 Jun 2007 02:39:21 -0700
Author:
|
Re: resizing image defect - strange border appears
Hi Sergei,
This problem is occuring because you are interpolating your image data to a
new size, but along the edges there are no pixels to interpolate and .NET
uses black pixels for these edges by default. To fix this you need to use an
ImageAttributes class in your DrawImage call....
Dim targetBitmap as new bitmap("...")
Dim sourceBitmap as new bitmap("...")
Using g As Graphics = Graphics.FromImage(targetBitmap)
sourceBitmap.SetResolution(g.DpiY, g.DpiY)
g.PixelOffsetMode = Drawing2D.PixelOffsetMode.HighQuality
Using ia As New ImageAttributes
' THE FOLLOWING LINES ENSURES GDI+ USES
' THE RIGHT PIXELS WHEN INTERPOLATING NEAR EDGES
ia.SetWrapMode(Drawing2D.WrapMode.TileFlipXY)
g.InterpolationMode = Drawing2D.InterpolationMode.HighQualityBicubic
g.DrawImage(sourceBitmap , New Rectangle(0, 0, width, height), 0, 0,
targetBitmap.Width, targetBitmap.Height, GraphicsUnit.Pixel, ia)
End Using
End Using
No more dark or edges when resizing.
Hope this helps.
-Blake
"Sergei Shelukhin" wrote in message
news:1183023561.338202.190190@n60g2000hse.googlegroups.com...
> Hi. I am implementing a code that takes an image of arbitrary size,
> then crops it to proprotion and resizes the result to some set
> thumbnail size.
> The problem is, when the image is resized there's a visible darker
> border along its edges (usually top and left), 1px wide, that is often
> perceived as black border when the image is shown (even though the
> border is actullay psrt of the image if you zoom in except that all
> pixels are sufficiently darker).
>
> Image are photographs and have no borders that could potentially be
> exaggerated by decreasing size; I use bicubic interpolation and smooth
> and high-quality everything.
>
> Thus far, I wrote a code that makes an image 2px wider and higher than
> required and then trims 1px from all sides.
> Is there any better way to get rid of this bug border? Why does it
> appear?
>
Date:Thu, 28 Jun 2007 21:21:00 +1000
Author:
|