开发者

Overlay two images of different types

I need to overlay the following images:

  1. System.Windows.Controls.Image image1
  2. System.Drawing.Bitmap image2;

I need the output to be of type System.Windows.Media.ImageSource.

I thought of the following way: Convert image2 into a 开发者_JS百科Bitmap and the overlay the two images using System.Drawing.Graphics, but I don't know how to convert image2.


One option is overlaying the two items in a grid as suggested above and then using this technique to render the control to a bitmap file.

Alternatively, you could convert both files to a bitmap and loop over them using the following code (this code combines the images by layering bitmap2 on bitmap1, assuming any white values in bitmap2 are transparency values - you can change this blending condition by altering one line):

Bitmap bitmap1;
Bitmap bitmap2;
Bitmap result;
for(int x = 0; x<bitmap1.Width;x++)
{
    for(int y=0; y<bitmap1.Height;y++)
    {
        //condition for choosing which pixel to pick - based on how you want to overlay them (this code assumes white is transparent)
        if(bitmap2.GetPixel(x,y) == Colors.White)
        {
            result.SetPixel(x,y) = bitmap.GetPixel(x,y);
        }
        else
        {
            result.SetPixel(x,y) = bitmap2.GetPixel(x,y);
        }
    }
}

This will give you the Bitmap result, which you can then do whatever you want with.

The first option is faster, as it is accelerated by the WPF render code, however I've heard people have struggled to force the rendered bitmap to not output immediately to a file, so if you want it in memory without loading the file again it's not your best bet.

The second option gives you much more control over how the images are combined, but is potentially slower.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜