Sign in to follow this  

[.net] Getting a rectangle drawn correctly around an image

Recommended Posts

Sounds easy. But it's not. So I want to be able to zoom in on image, so I just scale them (I could use the scaleTransform but I prefer to do it manually). So here's the code:
   g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.NearestNeighbor;
   g.DrawRectangle(Pens.Black, imagePosX - 1, imagePosY - 1, scaledWidth + 2, scaledHeight + 2);
   g.DrawImage(regionImage, new Rectangle(imagePosX, imagePosY, scaledWidth, scaledHeight), -0.5f, -0.5f, ((float)regionImage.Width) - 0.5f, ((float)regionImage.Height) - 0.5f, GraphicsUnit.Pixel);

I want to draw regionImage with a rectangle around it. Now it works on some sizes on scaledWidth & scaledHeight. But not all, and that's frustrating. I think that should work. The reason I take the source image from -0.5f, -0.5f to width -0.5, height - 0.5f is because of the NearestNeighbour thing. I don't want the first row and column of pixels to be drawn with half the size. Here is what happens when scaledWidth & Height is 96 (same as the source image) Any ideas? And remember I want it to work with all different scaledWidths and Heights, so I can't just tweak it to work. /MindWipe

Share this post

Link to post
Share on other sites
Ok, progress.

So by setting the boarder rectangles width to scaledWidth + 1 and scaledHeight + 1 seems to work better, which is kinda logical (IIRC rectangles are often drawn like that?).

And by setting the src rect from 0,0 to regionImage.Width, regionImage.Height it will work on scales from 2.0 - 1/inf.

But now I get two problems on higher scales:

1. The pic begins at -0.5px, -0.5px
2. The border to the right and bottom. Probably due to 1. (?)

Ideas how I can do this in the way I want it. I'm doing a sort of paint program, so I need the pixels to begin at 0,0.

Thanks in advance,

UPDATE: Got it fixed by changing the wrap mode and doing some tweaking

[Edited by - MindWipe on October 7, 2006 8:02:29 AM]

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this