copy ROI from one image and copy to on another image in wpf

后端 未结 1 1881
栀梦
栀梦 2021-01-28 02:25

I want to develop a function with the following signature:

 CopyImage(ImageSource inputImage, Point inTopLeft, Point InBottomRight, ImageSource outputImage, Poin         


        
相关标签:
1条回答
  • 2021-01-28 02:40

    Your method could look like this:

    private BitmapSource CopyRegion(
        BitmapSource sourceBitmap, Int32Rect sourceRect,
        BitmapSource targetBitmap, int targetX, int targetY)
    {
        if (sourceBitmap.Format != targetBitmap.Format)
        {
            throw new ArgumentException(
                "Source and target bitmap must have the same PixelFormat.");
        }
    
        var bytesPerPixel = (sourceBitmap.Format.BitsPerPixel + 7) / 8;
        var stride = bytesPerPixel * sourceRect.Width;
        var pixelBuffer = new byte[stride * sourceRect.Height];
        sourceBitmap.CopyPixels(sourceRect, pixelBuffer, stride, 0);
    
        var outputBitmap = new WriteableBitmap(targetBitmap);
        sourceRect.X = targetX;
        sourceRect.Y = targetY;
        outputBitmap.WritePixels(sourceRect, pixelBuffer, stride, 0);
    
        return outputBitmap;
    }
    
    0 讨论(0)
提交回复
热议问题