C# drawing Rectangle with precision coordinates. How? - c#

How do I draw a Rectangle in C# with precision coordinates?
Example:
Rectangle test1 = new Rectangle(X, Y, Width, Height);
Width and Height values seem to have to be integers.
Is it possible to give Rectangle, inch size coordinates somehow?
Rectangle test2 = new Rectangle(100, 50, 1.93inches, 0.52inches);
Thanks for any help.

You'd have to convert know the conversion between inches and pixels, which depends on the screen resolution. I suppose you could do this:
Graphics graphics = this.CreateGraphics();
var dpiX = graphics.DpiX;
var dpiY = graphics.DpiY;
var rectangle = new Rectangle(100, 50, Math.Round(1.93 * dpiX), Math.Round(0.52 * dpiY));

Related

Transforming Rectangle Coordinates to a Large Resolution Image

I'm using the following code to Transform a small rectangle coordinates to a larger one ie: A rectangle position on a small image to the same position on the larger resolution of the same image
Rectangle ConvertToLargeRect(Rectangle smallRect, Size largeImageSize, Size smallImageSize)
{
double xScale = (double)largeImageSize.Width / smallImageSize.Width;
double yScale = (double)largeImageSize.Height / smallImageSize.Height;
int x = (int)(smallRect.X * xScale + 0.5);
int y = (int)(smallRect.Y * yScale + 0.5);
int right = (int)(smallRect.Right * xScale + 0.5);
int bottom = (int)(smallRect.Bottom * yScale + 0.5);
return new Rectangle(x, y, right - x, bottom - y);
}
But there seems to be a problem with some images.The transformed rectangle coordinates seems to be off the image.
UPDATE:
img.Draw(rect, new Bgr(232, 3, 3), 2);
Rectangle transret= ConvertToLargeRect(rect, orgbitmap.Size, bit.Size);
target = new Bitmap(transret.Width, transret.Height);
using (Graphics g = Graphics.FromImage(target))
{
g.SmoothingMode = SmoothingMode.HighQuality;
g.DrawImage(orgbitmap, new Rectangle(0, 0, target.Width, target.Height),
transret, GraphicsUnit.Pixel);
}
Rectangle Drawn on small resolution Image
{X=190,Y=2,Width=226,Height=286}
Rectangle Transformed into Orginal Large Resolution Image {X=698,Y=7,Width=830,Height=931}
Original Image
First of all, if you resize the shape it shouldn't move position. That's not what one would expect out of enlarging a shape. This means the X,Y point of the top-left corner shouldn't be transformed.
Second, you shouldn't be adding 0.5 manually to operations, that's not a clean way to proceed. Use the ceiling function as suggested by #RezaAghaei
Third, you should not substract X/Y from the height/width, your calculations should be done as width * scale.
Please correct those mistakes, and if it doesn't work I'll update the answer with extra steps.

Drawing a Rectangle of Size 2X2 Inches -Inches to Pixel Conversion

I need to draw a rectangle that should be around the size 2X2 Inches when printed on a paper.
I know that i can draw a rectangle using
g.DrawRectangle(pen, 100,100, 100, 200);
This application will only be used in computers.How can i convert the inches to pixels properly so that i can get the desired dimensions when printed.
To make an image print in the right size by default, it needs to have the right combination of dpi and pixels.
Let's look at an example:
// aiming at 150dpi and 4x6 inches:
float dpi = 150;
float width = 4;
float height = 6;
using (Bitmap bmp = new Bitmap((int)(dpi * width), (int)(dpi * height)))
{
// first set the resolution
bmp.SetResolution(dpi, dpi);
// then create a suitable Graphics object:
using (Graphics G = Graphics.FromImage(bmp))
using (Pen pen = new Pen(Color.Orange))
{
pen.Alignment = System.Drawing.Drawing2D.PenAlignment.Center;
G.Clear(Color.FloralWhite);
// using pixels here:
Size sz = new System.Drawing.Size((int)dpi * 2 - 1, (int)dpi * 2 - 1);
G.DrawRectangle(pen, new Rectangle(new Point(0, 0), sz));
G.DrawRectangle(pen, new Rectangle(new Point(0, 300), sz));
G.DrawRectangle(pen, new Rectangle(new Point(0, 600), sz));
G.DrawRectangle(pen, new Rectangle(new Point(300, 0), sz));
G.DrawRectangle(pen, new Rectangle(new Point(300, 300), sz));
G.DrawRectangle(pen, new Rectangle(new Point(300, 600), sz));
// alternative code:
// we can also set the Graphics object to measure stuff in inches;
G.PageUnit = GraphicsUnit.Inch;
// or fractions of it, let's use 10th:
G.PageScale = 0.1f;
using (Pen pen2 = new Pen(Color.MediumPurple, 1f / dpi * G.PageScale))
{
// draw one rectangle offset by an inch:
G.DrawRectangle(pen2, 10f, 10f, 20f, 20f);
}
bmp.Save(#"D:\xxx.jpg", ImageFormat.Jpeg);
}
}
Note that I had to subtract 1 pixel from the drawn size as DrawRectangle overdraws by 1 pixel!
Note that the coordinates I draw at depend on the resolution! Also note how the jpeg format creates a lot of smeared colors. Pngcreates crisper results, especially once you print text..
Also note how I had to scale down the PenWidth in the alternative code!

Is EmguCV's System.Drawing.Rectangle usage by (Row,Column) or (X,Y)?

For example, in this method:
Image.Copy(Rectangle roi)
A new Image is returned, cropped according to the System.Drawing.Rectangle parameter. An example usage is:
var image = new Image<Gray, Byte>("filename.jpg");
var cropRectangle = new Rectangle(0, 200, 100, 10);
var cropped = image.Copy(cropRectangle);
OpenCV, EmguCV, and, hence, the Image class all operate with the coordinate system (Row, Column), but Rectangle operates with the more conventional (x, y) coordinate system.
How do those two coordinate systems map between each other? Should I create a Rectangle with x == 100 and y == 10 to crop starting at the 100th row and 10th column?
You just need to switch rows and cols.
// Crop from row (y) = 20, col (x) = 30, width = 40, height = 50
// Rectangle(x, y, width, height)
var cropRectangle = new Rectangle(30, 20, 40, 50);
The Rectangle class will describe a region of interest in the (x,y) coordinate system you are used to.
For example, the following Rectangle region will describe the yellow rectangle from the following image:
var bound = new Rectangle(0, 200, 100, 10);
// bound.X == 0
// bound.Y == 200
// bound.Width == 100
// bound.Height == 10

Fill a rectangle with an image using Mono Cairo Library

I am trying to fill an image inside a rectangle. I was able to set the image position correctly to the leftmost corner of the rectangle. However the scaling does not work as expected. Any help on this is appreciated. Below is my code. This is a 1290*1990 dimensions image.
Cairo.Rectangle imageRectangle = new Cairo.Rectangle(50, 100, width, height);
ctx.NewPath();
Cairo.ImageSurface imgSurface = new Cairo.ImageSurface("C:/Temp/Image.png");
ctx.SetSource(imgSurface, topLeftPoint); //topLeft is (50,100)
float xScale = (float)imageRectangle.Width / (float)imgSurface.Width;
float yScale = (float)imageRectangle.Height / (float)imgSurface.Height;
//Reposition the image to the rectangle origin
ctx.Translate(imageRectangle.X, imageRectangle.Y);
ctx.Scale(xScale, yScale);
ctx.Paint();
Thanks!!
I found the solution. I was setting the source at the wrong place. Below is the right code
Cairo.Rectangle imageRectangle = new Cairo.Rectangle(50, 100, width, height);
ctx.NewPath();
Cairo.ImageSurface imgSurface = new Cairo.ImageSurface("C:/Temp/Image.png");
float xScale = (float)imageRectangle.Width / (float)imgSurface.Width;
float yScale = (float)imageRectangle.Height / (float)imgSurface.Height;
//Reposition the image to the rectangle origin
ctx.Translate(imageRectangle.X, imageRectangle.Y);
ctx.Scale(xScale, yScale);
ctx.SetSource(imgSurface);
ctx.Paint();
Thanks!

I'm experiencing unexpected results from Graphics.DrawImage

To reproduce this issue, please create a 2x2 pixel black image in Microsoft Paint, saved as D:\small.png. Then create a new WinForms app in Visual Studio, with a no-margin PictureBox. Then use the following code:
void f6(Graphics g)
{
var img = Image.FromFile(#"d:\small3.png");
var srcRect = new Rectangle(0, 0, img.Width, img.Height);
int factor = 400;
var destRect = new Rectangle(0, 0, img.Width * factor, img.Height * factor);
g.DrawRectangle(new Pen(Color.Blue), destRect);
g.DrawImage(img, destRect, srcRect, GraphicsUnit.Pixel);
}
void pictureBox1_Paint(object sender, PaintEventArgs e)
{
f6(e.Graphics);
}
I expect the entire rectangle inside the blue margins be black while the output is as follows:
Why is this happening?
ok, thanks. i din't know about interpolation. now, let's change the code as following:
void f6(Graphics g)
{
var img = Image.FromFile(#"d:\small3.png");
var srcRect = new Rectangle(0, 0, img.Width, img.Height);
int factor = 200;
var destRect = new Rectangle(0, 0, img.Width * factor, img.Height * factor);
g.FillRectangle(new SolidBrush(Color.DarkCyan), pictureBox1.ClientRectangle);
g.InterpolationMode = InterpolationMode.NearestNeighbor;
g.DrawRectangle(new Pen(Color.Blue), destRect);
g.DrawImage(img, destRect, srcRect, GraphicsUnit.Pixel);
}
it produces the following result:
Result3
which is still unacceptable.
i've tried 60x60 images too. the problem is not because it's a 2x2 image. they produce the same effect. the problem is that why GDI+ decides not to fill the entire destRect with the entire srcRect?!
the original problem is that i've a big image tiled in smaller ones. i need adjacent tiles neither overlap nor seam exist between them. in C++, StretchBlt works properly. but it doesn't produce a smooth stretched image.
GDI+'s definition of the source rectangle is a bit odd.
(0,0) in the source image is actually the center of the upper-left pixel in the image. (width-1,height-1) is the center of the lower-right pixel in the image.
That means that the upper-left pixel is the rectangle from (-0.5,-0.5) to (0.5,0.5), and the lower-right pixel is the rectangle from (width-1.5,height-1.5) to (width-0.5,height-0.5). Thus, your source rectangle is actually outside the image by 0.5 pixels to the right and bottom.
So, you actually need a source rectangle of (-0.5, -0.5, img.Width, img.Height).
I guess you can also try setting PixelOffsetMode as Hans suggests. That would actually make sense of the behavior, but I wouldn't have expected it to apply to source rectangles.
Looks like the image was resized using bicubic interpolation. The normal Bicubic sizing algorithm normally requires 16 pixels to interpolate one, but you only have 4 in the image, so the remaining 12 pixels are never set, staying white.
Try changing the resizing method to nearest-neighbor. This is done by setting the InterpolationMode property of your Graphics object g to InterpolationMode.NearestNeighbor:
void f6(Graphics g)
{
var img = Image.FromFile(#"d:\small3.png");
var srcRect = new Rectangle(0, 0, img.Width, img.Height);
int factor = 400;
var destRect = new Rectangle(0, 0, img.Width * factor, img.Height * factor);
g.ImterpolatonMode = InterpolationMode.NearestNeighbor;
g.DrawRectangle(new Pen(Color.Blue), destRect);
g.DrawImage(img, destRect, srcRect, GraphicsUnit.Pixel);
}

Categories