本文介绍了如何在 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 图像中显示位图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-27 12:34