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

dotnet drawing api : Scaling (zooming) problem


Vinko
8/29/2006 10:04:53 PM
Hello!

I've created an image of 5x5 pixels size and put red diagonal line from
point 0,0 to point 5,5.
I want to zoom that image for any scale factor (in attached example 15x).
Interpolation mode
is set to NearestNeighbor. Why entirely image is not scaled properly, first
pixel and last pixel of
diagonal are hlaf size than others? Why?

int DefaultWidth = 5;
int DefaultHeight = 5;
int ScaleFactor = 15;

// image with diagonal
pictureBox1.Image = new Bitmap(DefaultWidth, DefaultHeight);
Graphics g1 = Graphics.FromImage(pictureBox1.Image);
g1.Clear(Color.White);
g1.DrawLine(new Pen(Color.Red, 1), new Point(0, 0), new Point(4,
4));
g1.Dispose();

// scaled image
int newWidth = DefaultWidth * ScaleFactor;
int newHeight = DefaultHeight * ScaleFactor;
pictureBox2.Image = new Bitmap(newWidth, newHeight);
Graphics g2 = Graphics.FromImage(pictureBox2.Image);
Rectangle srcRect = new Rectangle(0, 0, DefaultWidth,
DefaultHeight);
Rectangle destRect = new Rectangle(0, 0, newWidth, newHeight);
g2.InterpolationMode = InterpolationMode.NearestNeighbor;
g2.Clear(Color.White);
g2.DrawImage(pictureBox1.Image, destRect, srcRect,
GraphicsUnit.Pixel);
g2.Dispose();


Please help.

Thank you
Saso

Saso
8/29/2006 10:15:01 PM
I alreday tryed with Graphics.ScaleTransform(), but the problem is still there.
In this morning I found the solution on the following web page:

http://codemonkeyjas.blogspot.com/2006/07/problem-stretching-images-with-gdi.html

Is there any other explanation for DrawImage problem?

Regards
Saso

[quoted text, click to view]
SharpCoderMP
8/30/2006 12:09:04 AM
if you are drawing the image yourself then use Graphics.ScaleTransform()
method to draw it again with the same code but with a different scale.


[quoted text, click to view]
Bob Powell [MVP]
8/30/2006 7:38:33 PM
NearestNeighbor will do just that. If the pixel isn't on a boundary that can
be expressed as a direct multiple of the original then the scaling will
often miss a pixel or two after the draw takes place.

Just an aside, what's all the messing about with picturebox images for?

See the beginners guide to GDI+ for details. Follw the link from my site.
For tirades about PictureBoxes, see the GDI+ FAQ.

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

CodeMonkeyJas
11/7/2006 5:51:26 PM
Hi Saso,
I received an e-mail from microsoft regarding this error. Their view was that it would not be fixed in the next release and wouldn't be considered until it received more reports of the error.

They work on bugs by priority and the ones that effect the most people get the priority. Makes sense but sucks for this bug.

Jas

EggHeadCafe.com - .NET Developer Portal of Choice
AddThis Social Bookmark Button