首页 新闻 会员 周边

怎么复制BitmapImage

0
[已解决问题] 解决于 2023-06-16 21:23

如下, 我想把src复制到dest, 但是会报错. 请问, 我应该怎么操作才能正确复制呢?

        private void CopyImageButton_Click(object sender, RoutedEventArgs eventArgs)
        {
            BitmapImage src = new BitmapImage();
            src.BeginInit();
            MemoryStream ms = new MemoryStream(File.ReadAllBytes("D:\\Images\\test.png"));
            src.StreamSource = ms;
            src.CacheOption = BitmapCacheOption.OnLoad;
            src.EndInit();

            BitmapImage dest = new BitmapImage();
            dest.BeginInit();
            dest.StreamSource = new MemoryStream(new byte[src.StreamSource.Length], true);
            src.StreamSource.CopyTo(dest.StreamSource);
            dest.CacheOption = BitmapCacheOption.OnLoad;
            dest.EndInit();
        }

临雪的主页 临雪 | 初学一级 | 园豆:106
提问于:2023-06-12 11:04
< >
分享
最佳答案
1

在您的代码中,您正在尝试将一个位图图像 (src) 复制到另一个位图图像 (dest)。然而,在复制过程中,您可能会遇到问题,因为您没有正确处理内存流的位置。

以下是修正后的代码示例,展示了如何正确地复制位图图像:

csharp
Copy code
private void CopyImageButton_Click(object sender, RoutedEventArgs eventArgs)
{
BitmapImage src = new BitmapImage();
using (FileStream fileStream = new FileStream("D:\Images\test.png", FileMode.Open))
{
src.BeginInit();
src.CacheOption = BitmapCacheOption.OnLoad;
src.StreamSource = fileStream;
src.EndInit();
}

BitmapImage dest = new BitmapImage();
using (MemoryStream ms = new MemoryStream())
{
    BitmapEncoder encoder = new PngBitmapEncoder(); // 选择适当的编码器,例如PngBitmapEncoder
    encoder.Frames.Add(BitmapFrame.Create(src));
    encoder.Save(ms);
    ms.Position = 0;

    dest.BeginInit();
    dest.CacheOption = BitmapCacheOption.OnLoad;
    dest.StreamSource = ms;
    dest.EndInit();
}

}
在修正后的代码中,我们使用FileStream来打开源图像文件,并将其作为流源设置给src位图图像。在复制过程中,我们使用PngBitmapEncoder编码器来将源图像编码为PNG格式,并将其保存到内存流 (ms) 中。

然后,我们将内存流的位置 (ms.Position) 设置为0,以确保在将其作为流源设置给dest位图图像之前,流的位置已重置。

最后,我们使用正确的流源设置dest位图图像,并完成其初始化过程。

请注意,这只是一个示例,您可能需要根据您的具体需求进行适当的调整,例如选择适当的编码器和图像格式等。

奖励园豆:5
Technologyforgood | 大侠五级 |园豆:5992 | 2023-06-12 22:58

谢谢

临雪 | 园豆:106 (初学一级) | 2023-06-16 21:23
其他回答(1)
0

在 WPF 中,BitmapImage 类是用于显示图像的类,它并不支持直接复制图像。如果你想要复制图像,可以使用 Bitmap 类来实现。以下是示例代码:

private void CopyImageButton_Click(object sender, RoutedEventArgs eventArgs)
{
    Bitmap src = new Bitmap("D:\\Images\\test.png");
    Bitmap dest = new Bitmap(src.Width, src.Height);

    using (Graphics g = Graphics.FromImage(dest))
    {
        g.DrawImage(src, 0, 0);
    }

    // 保存复制后的图像
    dest.Save("D:\\Images\\test_copy.png");
}

在上述代码中,首先创建一个 Bitmap 对象来加载原始图像,然后创建一个新的 Bitmap 对象作为目标图像。接着使用 Graphics 类的 DrawImage 方法将原始图像复制到目标图像中。最后使用 Save 方法保存复制后的图像。
需要注意的是,如果你想要在 WPF 中显示复制后的图像,需要将 Bitmap 对象转换为 BitmapSource 对象,然后使用 Image 控件来显示图像。以下是示例代码:

private void CopyImageButton_Click(object sender, RoutedEventArgs eventArgs)
{
    Bitmap src = new Bitmap("D:\\Images\\test.png");
    Bitmap dest = new Bitmap(src.Width, src.Height);

    using (Graphics g = Graphics.FromImage(dest))
    {
        g.DrawImage(src, 0, 0);
    }

    // 将 Bitmap 对象转换为 BitmapSource 对象
    BitmapSource bitmapSource = Imaging.CreateBitmapSourceFromHBitmap(
        dest.GetHbitmap(),
        IntPtr.Zero,
        Int32Rect.Empty,
        BitmapSizeOptions.FromEmptyOptions());

    // 显示复制后的图像
    Image image = new Image();
    image.Source = bitmapSource;
    // 将 Image 控件添加到 UI 中
    // ...
}

在上述代码中,使用 Imaging 类的 CreateBitmapSourceFromHBitmap 方法将 Bitmap 对象转换为 BitmapSource 对象,然后将 BitmapSource 对象设置为 Image 控件的 Source 属性。

lanedm | 园豆:2381 (老鸟四级) | 2023-06-12 11:33
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册