从缩放的小尺寸图像获取相同的矩形位置 [英] Getting Same Rectangle Position from Scaled Small Size Image

查看:109
本文介绍了从缩放的小尺寸图像获取相同的矩形位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试处理大尺寸图像.由于处理需要太多时间才能完成,因此我需要在处理之前调整图像的大小.处理后,我在小尺寸图像上绘制了一个矩形.我该如何翻译该矩形与原始未缩放图像的坐标,即:在未缩放图像的相同位置绘制矩形.

I'm trying to process a large size image.Since the processing takes too much time to complete i'm resizing the image prior processing.After processing i'm drawing a rectangle on the small size image.How can i translate the the coordinates of this rectangle to original unscaled image ie:Draw the rectangle at the same position on the unscaled image.

我正在使用以下代码调整图片大小

I'm using the following code to resize the image

public static Size ResizeKeepAspect(Size CurrentDimensions, int maxWidth, int maxHeight)
{
    int newHeight = CurrentDimensions.Height;
    int newWidth = CurrentDimensions.Width;
    if (maxWidth > 0 && newWidth > maxWidth) //WidthResize
    {
        Decimal divider = Math.Abs((Decimal)newWidth / (Decimal)maxWidth);
        newWidth = maxWidth;
        newHeight = (int)Math.Round((Decimal)(newHeight / divider));
    }
    if (maxHeight > 0 && newHeight > maxHeight) //HeightResize
    {
        Decimal divider = Math.Abs((Decimal)newHeight / (Decimal)maxHeight);
        newHeight = maxHeight;
        newWidth = (int)Math.Round((Decimal)(newWidth / divider));
    }
    return new Size(newWidth, newHeight);
}

这就是我要实现的目标

推荐答案

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);
}

这篇关于从缩放的小尺寸图像获取相同的矩形位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆