0

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}

enter image description here

Rectangle Transformed into Orginal Large Resolution Image {X=698,Y=7,Width=830,Height=931}

Original Image

enter image description here

techno
  • 6,100
  • 16
  • 86
  • 192
  • Do you have any proof for your theory? (off-topic, but use `Rectangle.Ceiling` instead of those `+0.5` corrections.) – Reza Aghaei Jun 24 '18 at 09:21
  • @RezaAghaei Please see this reference answer https://stackoverflow.com/questions/47709943/getting-same-rectangle-position-from-scaled-small-size-image – techno Jun 24 '18 at 15:31
  • In fact I'm asking you to provide test data. Two images and the small rectangle and saying what is the expected while the method returns wrong result. – Reza Aghaei Jun 24 '18 at 15:38
  • @RezaAghaei Please see the update. – techno Jun 24 '18 at 16:18
  • @RezaAghaei Sorry if im annoying you.Did you take a look at the update? – techno Jun 25 '18 at 09:16
  • The `ConvertToLargeRect` is working well. But unfortunately I cannot understand your example. To be able to find the problem yourself, you should be able to first verify result of `ConvertToLargeRect`. By providing some known input and output data. – Reza Aghaei Jun 25 '18 at 09:24
  • @RezaAghaei I will update the example.. – techno Jun 25 '18 at 09:36
  • @RezaAghaei I was able to figure out the issue after some testing.Im resizing the image keeping aspect ratio using this code https://stackoverflow.com/q/35244688/848968 After this i crop out the unwanted white-space using this code https://stackoverflow.com/a/10392379/848968 .It seems the `Crop` function cuts out portion of the image when it has white background.I need to find new piece of code to resize the image keeping the aspect ratio. – techno Jun 25 '18 at 18:35

1 Answers1

0

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.

Héctor Álvarez
  • 494
  • 3
  • 18