Str*_*Gan 5 c# wpf bitmapsource embedded-resource .net-assembly
我的目标是在WPF窗口上绘制图像"someImage.png",它是嵌入式资源,在重写的OnRender方法中:
protected override void OnRender(System.Windows.Media.DrawingContext drawingContext)
{
base.OnRender(drawingContext);
drawingContext.DrawImage(ImageSource, Rect);
}
Run Code Online (Sandbox Code Playgroud)
我找到了将我的图像从资源转换为Stream的代码:
public BitmapSource GetSourceForOnRender()
{
System.Reflection.Assembly myAssembly = System.Reflection.Assembly.GetExecutingAssembly();
Stream myStream = myAssembly.GetManifestResourceStream("KisserConsole.someImage.png");
// What to do now?
return //BitmapSource
Run Code Online (Sandbox Code Playgroud)
}
但是我现在如何获得或创建BitmapSource?
您可以通过设置其StreamSource属性从流中创建BitmapImage :
public BitmapSource GetSourceForOnRender()
{
var assembly = System.Reflection.Assembly.GetExecutingAssembly();
var bitmap = new BitmapImage();
using (var stream =
assembly.GetManifestResourceStream("KisserConsole.someImage.png"))
{
bitmap.BeginInit();
bitmap.StreamSource = stream;
bitmap.CacheOption = BitmapCacheOption.OnLoad;
bitmap.EndInit();
}
return bitmap;
}
Run Code Online (Sandbox Code Playgroud)
也就是说,您通常会从资源文件包URI创建一个BitmapImage ,例如
new BitmapImage(new Uri(
"pack://application:,,,/KisserConsole.someImage.png"));
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3451 次 |
| 最近记录: |