【问题标题】:Copy Bitmap into other Bitmap with WPF使用 WPF 将位图复制到其他位图中
【发布时间】:2011-05-06 08:12:44
【问题描述】:

我需要用 WPF 将一个位图放到另一个位图的中心。

我设法创建了一张具有所需尺寸的空白图片,但我不明白如何将另一个 BitmapFrame 复制到其中。

BitmapSource bs = BitmapSource.Create(
    width, height,
    dpi, dpi,
    PixelFormats.Rgb24,
    null,
    bits,
    stride);

【问题讨论】:

    标签: wpf image bitmap


    【解决方案1】:

    您应该使用 WriteableBitmap 来写入像素缓冲区。使用 BitmapSource.CopyPixels 从 BitmapSource 复制到数组,然后使用 WriteableBitmap.WritePixels 将数组复制到 WriteableBitmap。

    这是一个注释实现

    XAML

    <Image Name="sourceImage" Height="50"
           Source="/WpfApplication1;component/Images/Gravitar.bmp" />
    <Image Name="targetImage" Height="50"/>
    

    代码

    // Quick and dirty, get the BitmapSource from an existing <Image> element
    // in the XAML
    BitmapSource source = sourceImage.Source as BitmapSource;
    
    // Calculate stride of source
    int stride = source.PixelWidth * (source.Format.BitsPerPixel / 8);
    
    // Create data array to hold source pixel data
    byte[] data = new byte[stride * source.PixelHeight];
    
    // Copy source image pixels to the data array
    source.CopyPixels(data, stride, 0);
    
    // Create WriteableBitmap to copy the pixel data to.      
    WriteableBitmap target = new WriteableBitmap(
      source.PixelWidth, 
      source.PixelHeight, 
      source.DpiX, source.DpiY, 
      source.Format, null);
    
    // Write the pixel data to the WriteableBitmap.
    target.WritePixels(
      new Int32Rect(0, 0, source.PixelWidth, source.PixelHeight), 
      data, stride, 0);
    
    // Set the WriteableBitmap as the source for the <Image> element 
    // in XAML so you can see the result of the copy
    targetImage.Source = target;
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-08-20
    • 1970-01-01
    • 2012-03-08
    • 2011-08-10
    • 1970-01-01
    • 1970-01-01
    • 2011-07-30
    • 1970-01-01
    相关资源
    最近更新 更多