C# 如何在WPF图像中显示位图
我想实现一个图像编辑程序,但我不能在WPF中显示位图。 对于一般编辑,我需要一个位图。但我无法在图像中显示这一点C# 如何在WPF图像中显示位图,c#,wpf,image,bitmap,C#,Wpf,Image,Bitmap,我想实现一个图像编辑程序,但我不能在WPF中显示位图。 对于一般编辑,我需要一个位图。但我无法在图像中显示这一点 private void MenuItemOpen_Click(object sender, RoutedEventArgs e) { OpenFileDialog openfiledialog = new OpenFileDialog(); openfiledialog.Title = "Open Image"; openfiledialog.Filter
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中设置图片。像这样:
Image.Source = image;
我真的需要一个位图来获得特殊像素的颜色我需要一个简单的代码剪辑
谢谢你的帮助这应该可以:
ImageSource imgSource = new BitmapImage(new Uri("HERE GOES YOUR URI"));
image1.Source = imgSource; //image1 is your control
如果需要位图类,请尝试使用以下方法:
public ImageSource imageSourceForImageControl(Bitmap yourBitmap)
{
ImageSourceConverter c = new ImageSourceConverter();
return (ImageSource)c.ConvertFrom(yourBitmap);
}
我已使用此“现在剪掉”将位图转换为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;
}
}
如果您想保留
System.Drawing.Bitamp
而不是使用,请检查问题:这对我不起作用。我需要位图类来获取特殊像素的颜色。检查我的答案,我更新了它。(ImageSource)c.ConvertFrom(yourBitmap)
导致NullReferenceException
我得到NullReferenceException,为什么有人在EndInit
之后将bitmapimage.Freeze()
放在这里?