简体   繁体   English

从Bitmap获取ImageSource?

[英]Get ImageSource from Bitmap?

I want to set a background image for my form/window like this guy but instead of an image file on disk I've got a System.Drawing.Bitmap in memory. 我想为我的表单/窗口设置一个背景图像, 就像这个人而不是磁盘上的图像文件我在内存中有一个System.Drawing.Bitmap

I need to do something like this: 我需要做这样的事情:

this.Background = new ImageBrush(new BitmapImage(bmp));

Except BitmapImage won't take a Bitmap , nor will ImageBrush and I'm not sure if any of the others will. 除了BitmapImage不会采用BitmapImageBrush和我也不确定是否会有其他人。 There's one called BitmapCacheBrush but I don't know what that does. 有一个名为BitmapCacheBrush但我不知道它是做什么的。

Nevermind, I figured it out. 没关系,我明白了。

public static Brush CreateBrushFromBitmap(Bitmap bmp)
{
    return new ImageBrush(Imaging.CreateBitmapSourceFromHBitmap(bmp.GetHbitmap(), IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions()));
}

credit 信用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM