Groups | Blog | Home
all groups > dotnet drawing api > january 2006 >

dotnet drawing api : DrawImage with sharp edges


Andreas Zita
1/28/2006 12:03:13 PM
Hi

Im using DrawImage to resize large images to smaller ones.

I use InterpolationMode =
System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic to obtain a
good downsampling of the pixels in the image.

However no matter what I do I get visible black lines (or whatever
background color of the canvas bitmap is set to) at the left and top side of
the resulting image. I believe this is caused by the interpolation algorithm
which is using the backgroundcolor to antialias the edges of whatever is
drawn to the canvas. But in my case I don't want to antialias to the
background at all! I should be a simple matter to disregard the background
with some boundary conditions? Is there any solution to this problem? Or do
I have to write my own image resize code to get sharp edges?

/Andreas

Bob Powell [MVP]
1/30/2006 3:28:57 PM
When you say that you're resizing an image and the background canvas is also
sampled I think that you're suggesting that you're resizing directly from
the screen or from a back-buffer bitmap that you paint to the screen. This
is probably a bad idea for the reasons that you've already discovered.

You should be resizing the original image bits in some off-screen operation.

Also, try using the HighQualityBilinear interpolation mode. I find that this
tends to work better and produce a more pleasing effect.

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



[quoted text, click to view]

Doug Forster
1/31/2006 12:00:00 AM
Hi Andreas,

This is a long standing bug in GDI+ caused I suppose by some lazy
implementer failing to deal properly with the boundary situation. I have
only found two workarounds out in the unmananged world. One fudge is to
increase the source image size by replicating pixels around the edge (I use
this for targeting Win9X). The other approach for Win 2000 onwards is to use
GDI with StretchBltMode HALFTONE. This gives comparable results (much faster
as well) without the bug. I don't know how you would accomplish either of
these workarounds in the managed world but no doubt it is possible if rather
messy.
You could (as you suggest) implement the resize yourself but I suspect it
would be rather slow.

Cheers
Doug Forster


[quoted text, click to view]

AddThis Social Bookmark Button