【问题标题】:Get Rectangle of Image JPG and generate thumbnail in rectangle获取图片JPG的矩形并生成矩形缩略图
【发布时间】:2011-04-01 08:13:41
【问题描述】:

我试着解释一下:我有一张 JPG 图片 (img A)。

这个 A.jpg 有内容 - 颜色,它是一张人的照片 - 和一个白色的小矩形(颜色为白色;人的头部是一个白色的矩形)。我需要获取 A.jpg 中矩形的位置。

然后,我有另一个 B.jpg 图像,更多的少;我将生成 B.jpg 的缩略图,带有 Rectangle 尺寸(A.jpg 中的白色矩形)。

最后,我会生成新的图片:C.jpg,在A.jpg的矩形中有A.jpg和B.jpg。

任何建议,任何示例代码?我只使用 vs 2008、.net 3.5、GDI+。

【问题讨论】:

  • 基本上你想给图片 A 中的人添加一张新面孔 B

标签: .net image-processing position thumbnails


【解决方案1】:

对于 A 问题,您可以计算每列和每行中白色像素的数量。白色像素数最多的列/行是矩形边框所在的位置。 (假设矩形边与图片的边平行)

对于 B 和 C,提示以

开头
Bitmap aImage; // Initialize with your images
using (Graphics g = Graphics.FromImage(aImage))
{
    // Do stuff
}

然后您可以找到并重载Graphics.DrawImage 以缩放和绘制您的图像。

要计算像素数,您可以使用GetPixel 方法。

// Sketchy code
// Calculate each column in sum[x]
[x,y] = b.Size;
for(x ...)
    for(y ..)
        if (aImage.GetPixel(x, y) == Color.White)
            sum[x]++;

【讨论】:

    【解决方案2】:

    这是一个 sn-p 将一个图像排在另一个图像上。 (没有信用i took it from here

    Bitmap bmp = Bitmap.FromFile(initialFileName);
    
    // This draws another image as an overlay on top of bmp in memory.
    // There are additional forms of DrawImage; there are ways to fully specify the
    // source and destination rectangles. Here, we just draw the overlay at position (0,0).
    
    using (Graphics g = Graphics.FromImage(bmp))
    {
       g.DrawImage(Bitmap.FromFile(overlayFileName), 0, 0);
    }
    bmp.Save(saveAsFileName, System.Drawing.Imaging.ImageFormat.Png);
    

    现在介绍如何在图像中定位一个大的白色矩形?这有点棘手。

    有一个library which can do this for you

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-03
      • 2020-03-06
      • 1970-01-01
      • 1970-01-01
      • 2015-01-18
      • 2013-01-14
      相关资源
      最近更新 更多