How to Display a Bitmap in a WPF Image(如何在 WPF 图像中显示位图)
问题描述
我想实现一个图像编辑程序,但我无法在我的 WPF 中显示 Bitmap.对于一般编辑,我需要一个位图.但我无法在图像中显示.
I want to implement a image editing program, but I can not display the Bitmap in my WPF. For the general editing I need a Bitmap. But I can not display that in a Image.
private void MenuItemOpen_Click(object sender, RoutedEventArgs e)
{
OpenFileDialog openfiledialog = new OpenFileDialog();
openfiledialog.Title = "Open Image";
openfiledialog.Filter = "Image File|*.bmp; *.gif; *.jpg; *.jpeg; *.png;";
if (openfiledialog.ShowDialog() == true)
{
image = new Bitmap(openfiledialog.FileName);
}
}
我将带有 OpenFileDialog 的图像加载到位图中.现在我想在我的 WPF 中设置图片.像这样:
I load the Image with a OpenFileDialog into the Bitmap. Now I want to set the picture in my WPF. Like so:
Image.Source = image;
我真的需要一个位图来获取特殊像素的颜色!我需要剪下一个简单的代码.
I really need a Bitmap to get the color of a special pixel! I need a simple code snipped.
感谢您的帮助!
推荐答案
我现在使用这个剪下的 Bitmap 转换为 ImageSource:
I have used this snipped now to convert the Bitmap to a ImageSource:
BitmapImage BitmapToImageSource(Bitmap bitmap)
{
using (MemoryStream memory = new MemoryStream())
{
bitmap.Save(memory, System.Drawing.Imaging.ImageFormat.Bmp);
memory.Position = 0;
BitmapImage bitmapimage = new BitmapImage();
bitmapimage.BeginInit();
bitmapimage.StreamSource = memory;
bitmapimage.CacheOption = BitmapCacheOption.OnLoad;
bitmapimage.EndInit();
return bitmapimage;
}
}
这篇关于如何在 WPF 图像中显示位图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在 WPF 图像中显示位图
基础教程推荐
- 在 VS2010 中的 Post Build 事件中将 bin 文件复制到物 2022-01-01
- 如何动态获取文本框中datagridview列的总和 2022-01-01
- 从 VS 2017 .NET Core 项目的发布目录中排除文件 2022-01-01
- 首先创建代码,多对多,关联表中的附加字段 2022-01-01
- 是否可以在 asp classic 和 asp.net 之间共享会话状态 2022-01-01
- 经典 Asp 中的 ResolveUrl/Url.Content 等效项 2022-01-01
- 全局 ASAX - 获取服务器名称 2022-01-01
- 错误“此流不支持搜索操作"在 C# 中 2022-01-01
- JSON.NET 中基于属性的类型解析 2022-01-01
- 将事件 TextChanged 分配给表单中的所有文本框 2022-01-01
