`

c# - Convert from System.Drawing.Image to System.WIndows.Media.ImageSource

阅读更多

In Previous discussion, we have seen the code Convert from System.Drawing.Icon to System.Media.ImageSource and vice versa 

 

 

System.Windows.Drawing.Image is the base class of  and System.Drawing.Bitmap, so that you can expect that the method for System.Drawing.Bitmap also applies for the System.Drawing.Image.. 

 

NOTE: the System.Drawing.Icon has no relationship with System.Drawing.Bitmap or System.Drawing.Image, there exist some conversion to Icon and vice versa, but don't expect the same approach can work with System.Drawing.Icon smoothly.

 

 

This introduce yet another way to convert from System.Drawing.Image to System.Windows.Media.ImageSource.

 

 

public static System.Windows.Media.ImageSource ConvertDrawingImage2MediaImageSource(System.Drawing.Image image)
    {
      using (var ms = new MemoryStream())
      {
        var bitmap = new System.Windows.Media.Imaging.BitmapImage();
        bitmap.BeginInit();

        image.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
        ms.Seek(0, System.IO.SeekOrigin.Begin);
        bitmap.StreamSource = ms;
        bitmap.EndInit();
        return bitmap;
      }
    }
 
分享到:
评论

相关推荐

    System.Drawing.Image.FromStream 出现参数无效的解决方法及图片类型判断

    在.NET框架中,`System.Drawing.Image.FromStream`方法被广泛用于从流中加载图像,例如从网络、内存或文件读取。然而,有时在尝试使用此方法时可能会遇到“参数无效”的异常,这通常是由于流中的数据不正确或格式不...

    System.Drawing.PSD-master.zip_HIX_System.Drawing.PSD_c# psd to j

    首先,我们要理解C#中的`System.Drawing`命名空间,它是.NET Framework的一部分,提供了处理2D图形、图像和颜色的基本功能。然而,`System.Drawing`并没有直接支持PSD文件的解析。为了解析PSD文件,我们需要借助第三...

    C#System.Drawing.dll类库文件

    C#System.Drawing.dll,导入unityPlugins文件下,system.drawing.dll文件下载,解决找不到system.drawing.dll的问题

    unity 可以使用的System.Drawing.dll

    `System.Drawing`命名空间包含了处理图像、图形和颜色的一系列类,如Bitmap、Graphics、Image、Pen、Color等。在Unity中,虽然默认并不直接支持`System.Drawing`,但可以通过引入第三方库或自定义实现来使用。以下是...

    使用C# System.Drawing 绘制一张处方图片

    在C#编程环境中,System.Drawing命名空间提供了丰富的类和方法,用于进行图形绘制和图像处理。这个场景下,我们将在一个项目中利用这些工具来创建一张包含文字和多图层叠加效果的处方图片。让我们深入了解一下如何...

    unity可以用的System.Drawing.dll.zip

    在Windows环境下,Unity可以利用这个库来处理图像,特别是在C#脚本中,通过System.Drawing命名空间,开发者可以直接操作位图、创建图形对象并进行绘制。 然而,对于Mac用户来说,情况就有所不同了。由于Unity在Mac ...

    unity3d System.Drawing

    在Unity中,我们通常使用C#作为主要的编程语言,而`System.Drawing`是.NET框架的一个部分,主要用于处理图形、图像和文本。在Unity3D中使用`System.Drawing`库,可以实现一些在原生Unity API之外的图像处理功能。 `...

    由直线、曲线创建图形,using System.Drawing.Drawing2D;的应用例子,C#源代码 Point[]

    由直线、曲线创建图形,using System.Drawing.Drawing2D;的应用例子,C#源代码 Point[] points = { new Point(40, 60), new Point(50, 70), new Point(30, 90)}; GraphicsPath path =...

    Unity可使用的System.Drawing.dll,

    请注意,由于`System.Drawing.dll`是为Windows系统设计的,因此在其他操作系统上(如iOS、Android或Linux)可能无法直接使用。对于跨平台的项目,可能需要寻找替代方案,如Unity的内置2D系统或第三方库。 总之,`...

    System.Drawing.dll

    C#System.Drawing.dll,导入unityPlugins文件下,使用

    winform下Bitmap转wpf下ImageSource

    在Winform中,我们通常使用`System.Drawing.Bitmap`类来处理图像,而在WPF中,图像数据则被表示为`System.Windows.Media.ImageSource`。当你需要在WPF应用中显示一个在Winform中创建或处理过的`Bitmap`对象时,就...

    C#System.Drawing.Color的颜色对照表

    C#System.Drawing.Color的颜色对照表 有图有说明 转载自 http://www.cnblogs.com/xpvincent/archive/2012/12/17/2821665.html

    System.Drawing.Design 微软.net 框架源码下载

    System.Drawing.Design 微软.net 框架源码下载 System.Drawing.Design 微软.net 框架源码下载 System.Drawing.Design 微软.net 框架源码下载

    Unity创建表格Aspose.Words插件和system.drawing插入图片

    Image image = Image.FromFile("path_to_image.jpg"); // 将图片转换为MemoryStream,以便于Aspose.Words处理 MemoryStream imageStream = new MemoryStream(); image.Save(imageStream, System.Drawing.Imaging....

    System.Drawing.dll unity可以

    2. 引用System.Drawing命名空间:在C#脚本中,你需要包含`using System.Drawing;`来访问该库中的类和接口,如Bitmap、Graphics、Color等。 3. 使用System.Drawing功能:例如,你可以创建一个Bitmap对象来加载或新建...

    System.Drawing 微软 .net 框架下载

    System.Drawing是微软.NET框架中的一个核心库,主要提供了一组丰富的图形编程接口,使得开发者能够进行图像处理、绘制图形、创建图表以及与Windows GDI+(Graphics Device Interface Plus)进行交互。这个库广泛应用...

    Unity可用的System.Drawing.dll

    C#的System.Drawing.dll 可在Unity中使用的版本

    System.Drawing.rar

    标题 "System.Drawing.rar" 暗示这是一个与.NET框架中的System.Drawing命名空间相关的资源压缩包。这个命名空间在C#编程中非常重要,特别是在图形处理、图像操作和Windows应用程序的用户界面设计方面。当开发者遇到...

    sqlite-netFx40-setup-bundle-x86-2010-1.0.97.0.exe

    C#连接SQLite System.data.SQLite.dll 安装文件,安装完后在安装路径找到System.data.SQLite.dll引入C#的工程中

Global site tag (gtag.js) - Google Analytics