如何在 C# 中复制 Bitmap.Image

发布于 2024-12-27 04:11:25 字数 1850 浏览 1 评论 0原文

我在从内存流保存图像时遇到一些麻烦。

这是我的代码:

MemoryStream ms = new MemoryStream(onimg);

if (ms.Length > 0)
{
    Bitmap bm = new Bitmap(ms);
    returnImage = (Image)bm.Clone();
}
ms.Close();
returnImage.Save(@"C:\img.jpeg");

returnImage.Save 上,我遇到以下异常:

GDI+ 中发生一般错误。

如果我不关闭 MemoryStream 一切都可以,但在一段时间后需要大量内存。

我该怎么做?

编辑:保存只是演示..我确实需要 returnImage 将其放置在 ObservableCollection 中,并在我需要将其转换为 System.Windows.Media.Imaging.BitmapImage() 时显示在窗口中;

[ValueConversion(typeof(System.Drawing.Image), typeof(System.Windows.Media.ImageSource))]
public class ImageConverter : IValueConverter
{
    public object Convert(object value, Type targetType,
        object parameter, CultureInfo culture)
    {
            // empty images are empty...
            if (value == null) { return null; }

            var image = (System.Drawing.Image)value;
            // Winforms Image we want to get the WPF Image from...
            var bitmap = new System.Windows.Media.Imaging.BitmapImage();
            bitmap.BeginInit();
            MemoryStream memoryStream = new MemoryStream();
            // Save to a memory stream...
            image.Save(memoryStream, ImageFormat.Bmp);
            // Rewind the stream...
            memoryStream.Seek(0, System.IO.SeekOrigin.Begin);
            bitmap.StreamSource = memoryStream;
            bitmap.EndInit();
            return bitmap;
    }

    public object ConvertBack(object value, Type targetType,
        object parameter, CultureInfo culture)
    {
        return null;
    }
}

当我这样做时 XAML

<DataTemplate>
   <Image Width="32" Height="32" Source="{ Binding Thumb, Converter={StaticResource    imageConverter} }" />
</DataTemplate>

I have some troubles with saving a image from memorystream.

Here is my code:

MemoryStream ms = new MemoryStream(onimg);

if (ms.Length > 0)
{
    Bitmap bm = new Bitmap(ms);
    returnImage = (Image)bm.Clone();
}
ms.Close();
returnImage.Save(@"C:\img.jpeg");

And on returnImage.Save i have the following exception:

A generic error occurred in GDI+.

If I do not close the MemoryStream all is OK but that requires lot of memory after some time.

How can I do this?

EDIT:That Save is only demonstration.. I really need returnImage for place it in ObservableCollection and display in window when i Need it convert to System.Windows.Media.Imaging.BitmapImage();

[ValueConversion(typeof(System.Drawing.Image), typeof(System.Windows.Media.ImageSource))]
public class ImageConverter : IValueConverter
{
    public object Convert(object value, Type targetType,
        object parameter, CultureInfo culture)
    {
            // empty images are empty...
            if (value == null) { return null; }

            var image = (System.Drawing.Image)value;
            // Winforms Image we want to get the WPF Image from...
            var bitmap = new System.Windows.Media.Imaging.BitmapImage();
            bitmap.BeginInit();
            MemoryStream memoryStream = new MemoryStream();
            // Save to a memory stream...
            image.Save(memoryStream, ImageFormat.Bmp);
            // Rewind the stream...
            memoryStream.Seek(0, System.IO.SeekOrigin.Begin);
            bitmap.StreamSource = memoryStream;
            bitmap.EndInit();
            return bitmap;
    }

    public object ConvertBack(object value, Type targetType,
        object parameter, CultureInfo culture)
    {
        return null;
    }
}

And XAML when i do this

<DataTemplate>
   <Image Width="32" Height="32" Source="{ Binding Thumb, Converter={StaticResource    imageConverter} }" />
</DataTemplate>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

贪恋 2025-01-03 04:11:25

根据文档

您必须在位图的生命周期内保持流打开。

Bitmap 需要将其数据存储在同一位置,并且我推断(尽管没有证据)Bitmap 不会复制数据,而是使用流并对其保持锁定。

此外,没有证据表明克隆将使用字节表示的副本创建新的位图。你的测试用例表明这不是一个案例。

因此,恐怕您需要在图像的生命周期内保持流开放。它需要内存,确实如此,但是如果位图复制了数据,您仍然需要该内存来表示位图。因此,开放流不会消耗更多内存(如果我之前的推论是正确的)。

如果您确实想克服位图对原始内存流的依赖,则需要在新位图上绘制原始位图,而不是像 此处
但这会影响性能,如果保留原始流不是一个好主意,我最好重新分析,只需确保在处理位图时将其关闭即可。

According to documentation:

You must keep the stream open for the lifetime of the Bitmap.

Bitmap needs to store its data samewhere, and I deduce (although have no proof for this) that Bitmap does not copy the data, instead using the stream, keeping a lock on it.

Also, there is no evidence that Clone will create new bitmap with the copy of byte representation. And your testcase suggests it is not a case.

Therefore, I am afraid you need to keep your stream open for the lifetime of your image. It requires memory, true, but otherwise if Bitmap copied the data, you will still need that memory for the bitmap representation. Therefore, no more memory is consumed with open stream (if my previous deduction was true).

If you really want to overcome the bitmap's dependency on the original memory stream, you will need to draw the orginal bitmap on the new one instead of cloning like here.
But this will impact the performance, I would better re-analyze if it is not a good idea to keep the original stream, just making sure it is closed when bitmap is disposed.

素罗衫 2025-01-03 04:11:25

保存到磁盘基本上不是克隆吗?

using (MemoryStream ms = new MemoryStream(onimg))
{
    if (ms.Length > 0)
    {
        using (Bitmap bm = new Bitmap(ms))
        {
            bm.Save(@"C:\img.jpeg");
        }
    }
}

Isn't a save to disk basically a clone?

using (MemoryStream ms = new MemoryStream(onimg))
{
    if (ms.Length > 0)
    {
        using (Bitmap bm = new Bitmap(ms))
        {
            bm.Save(@"C:\img.jpeg");
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文