将位图中的不需要的图像大小调整为ImageSource

时间:2016-08-12 21:26:17

标签: image bitmap size emgucv imagesource

我正在通过WPF编程(c#)进行图像处理(在emgu cv中)。在我的应用程序中,我使用以下代码打开图像:

OpenFileDialog d = new OpenFileDialog();

if(d.ShowDialog() == true)
{
    Bitmap b = new Bitmap(d.FileName);
    img1.Source = Util.Convert(b);
}

我使用下面的代码将Bitmap转换为ImageSource,反之亦然:

public static Bitmap ImageSourceToBitmap(ImageSource imageSource)
{
    BitmapSource bitmapSource = (BitmapSource)imageSource;

    MemoryStream mse = new MemoryStream();
    BmpBitmapEncoder encoder = new BmpBitmapEncoder();
    encoder.Frames.Add(BitmapFrame.Create(bitmapSource));
    encoder.Save(mse);

    System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(mse);

    return bitmap;
}

public static BitmapImage Convert(Bitmap src)
{
    MemoryStream ms = new MemoryStream();
    ((System.Drawing.Bitmap)src).Save(ms, System.Drawing.Imaging.ImageFormat.Png);

    BitmapImage image = new BitmapImage();
    image.BeginInit();
    ms.Seek(0, SeekOrigin.Begin);
    image.StreamSource = ms;
    image.EndInit();
    return image;
}

当我从按钮使用图像时,图像的大小会发生变化(我的转换会发生奇怪的行为)。按钮的代码事件是:

Image<Gray, byte> im = new Image<Gray, byte>(Util.ImageSourceToBitmap(img1.Source));
im._EqualizeHist();

img1.Source = Util.Convert(im.Bitmap);

我还设置了stretch = None

问题是什么?

1 个答案:

答案 0 :(得分:0)

你在显示什么图像?这可能会给你一些调整大小。我通常使用画布和缩放,所以我的图像总是相同的大小。

你的转换看起来很奇怪。请尝试以下方法:Convert Bitmaps

由于BitmapImage是BitmapSource的派生,因此这应该可以正常工作。我怀疑它也会更快。

道格