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

This topic is 4275 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

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

MindWipe

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

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

• 10
• 9
• 48
• 12
• 10
• ### Forum Statistics

• Total Topics
631384
• Total Posts
2999701
×