반응형
public static RenderTargetBitmap MergeImage(BitmapSource img1, BitmapSource img2)
{
    var group = new DrawingGroup();
    group.Children.Add(new ImageDrawing(img1, new Rect(0, 0, img1.PixelWidth, img1.PixelHeight)));
    group.Children.Add(new ImageDrawing(img2, new Rect(0, 0, img2.PixelWidth, img2.PixelHeight)));

    var drawingVisual = new DrawingVisual();

    using (var drawingContext = drawingVisual.RenderOpen())
    {
        drawingContext.DrawDrawing(new DrawingImage(group).Drawing);
    }

    var bitmap = new RenderTargetBitmap(img1.PixelWidth, img1.PixelHeight, 96, 96, PixelFormats.Pbgra32);
    bitmap.Render(drawingVisual);

    return bitmap;
}
public static RenderTargetBitmap MergeImage(List<BitmapSource> imgList)
{
    var group = new DrawingGroup();

    for (int i = 0; i < imgList.Count; i++)
    {          
        group.Children.Add(new ImageDrawing(imgList[i], new Rect(0, imgList[i].PixelHeight * i, imgList[i].PixelWidth, imgList[i].PixelHeight)));
    }

    var drawingVisual = new DrawingVisual();

    using (var drawingContext = drawingVisual.RenderOpen())
    {
        drawingContext.DrawDrawing(new DrawingImage(group).Drawing);
    }

    var bitmap = new RenderTargetBitmap(imgList[0].PixelWidth, imgList[0].PixelHeight * imgList.Count, 96, 96, PixelFormats.Pbgra32);
    bitmap.Render(drawingVisual);

    return bitmap;
}
반응형

+ Recent posts