views:

35

answers:

2

I try explain: I have a JPG image (img A).

This A.jpg has contents -colors, it's a picture of persons- and a one more little white rectangle (color white; the head of person is a white rectangle). I need get the position of rectangle in A.jpg.

Then, I have another B.jpg image, more little; and I'll generate thumbnail of B.jpg , with Rectangle dimensions (of white rectangle in A.jpg).

Finally, I'll generate new image: C.jpg, will has A.jpg, and B.jpg in rectangle of A.jpg.

any suggestions, any sample code ? I use vs 2008, .net 3.5, GDI+ only.

+2  A: 

For the A problem, you could count the number of white pixels in each column and each row. The columns/rows with the highest number of white pixels are where the borders of your rectangle are. (Assuming that the rectangle sides are parallel to the sides of the image)

For B and C the hint is to start with

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

And then you can find and overload of Graphics.DrawImage to scale and draw your images atop of each other.

To count the number of pixels you can use the GetPixel method.

// 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]++;
Albin Sunnanbo
Albin, any sample code for the A problem ? thx
alhambraeidos
+1  A: 

Here is a snippet on draing an image over another image. (No credit 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);

Now on how to locate a large white rectangle inside an image? This bit is a little trickier.

There is a library which can do this for you

Eric