2010-08-19 72 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