Image对象在WPF中创建的ImageBrush

Image对象在WPF中创建的ImageBrush

本文介绍了如何从System.Drawing.Image对象在WPF中创建的ImageBrush?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在为System.Drawing.Image对象的形象和我需要创建一个对象的ImageBrush(用于矩形的WPF例如填充属性)从它。我想应该有办法做到这一点,但我不能找到一个。


解决方案

  VAR图像= System.Drawing.Image.FromFile(...); //或者是任何一个来自
VAR位=新System.Drawing.Bitmap(图片);
变种的BitmapSource = Imaging.CreateBitmapSourceFromHBitmap(bitmap.GetHbitmap(),
IntPtr.Zero,
Int32Rect.Empty,
BitmapSizeOptions.FromEmptyOptions()
);
bitmap.Dispose();
变种刷=新的ImageBrush(的BitmapSource);

这个解决方案,但是,那并不释放手柄的存储器。有关如何取出内存泄漏看到

$ B信息$ b

I have image in System.Drawing.Image object and I need to create an ImageBrush object (used for Fill property of Rectangle in WPF for example) from it. I guess there should be a way to do this, but I can't find one.

解决方案
      var image = System.Drawing.Image.FromFile("..."); // or wherever it comes from
      var bitmap = new System.Drawing.Bitmap(image);
      var bitmapSource = Imaging.CreateBitmapSourceFromHBitmap(bitmap.GetHbitmap(),
                                                                            IntPtr.Zero,
                                                                            Int32Rect.Empty,
                                                                            BitmapSizeOptions.FromEmptyOptions()
            );
      bitmap.Dispose();
      var brush = new ImageBrush(bitmapSource);

This solution, however, doesnt free the memory of the handle. For information on how to remove the memory leak see WPF CreateBitmapSourceFromHBitmap memory leak

这篇关于如何从System.Drawing.Image对象在WPF中创建的ImageBrush?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 22:09