2010-08-19 57 views
1

我尝试解释:我有一个JPG图像(img A)。获取图像JPG的矩形并在矩形中生成缩略图

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

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

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

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

+0

基本上你想提出一个新的B面的人在图像A – Eric 2010-08-20 05:24:20

回答

2

对于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]++; 
+0

阿尔滨,对于存在问题的所有示例代码?谢谢 – Kiquenet 2010-08-19 21:36:04

1

这是关于在另一个图像上显示图像的代码片段。 (没有信用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