在wpf中调整大小并将byte []绑定到图像的最佳方法

时间:2015-01-27 07:30:34

标签: c# wpf bitmap

我有byte[]存储了一些未知大小的图片。我需要将此位图的大小调整为160 x 160并绑定到Wpf窗口。

我应该将byte[]转换为Bitmap,调整大小,从中生成BitmapSource然后在wpf中使用吗?

//result of this method binds to wpf
private static BitmapSource SetImage(byte[] dataBytes)
{
        var picture = BitmapHelper.ByteToBitmap(dataBytes);

        var resizedPicture = BitmapHelper.Resize(picture, 160, 160);

        var bitmapSource = BitmapHelper.GetSourceFromBitmap(resizedPicture);

        bitmapSource.Freeze();

        return bitmapSource; 
}

// BitmapHelper类:

    public static Bitmap ByteToBitmap(byte[] byteArray)
    {
        using (var ms = new MemoryStream(byteArray))
        {
            var img = (Bitmap)Image.FromStream(ms);
            return img;
        }
    }

    internal static Bitmap Resize(Bitmap bitmap, int width, int height)
    {
        var newImage = new Bitmap(width, height);
        using (var gr = Graphics.FromImage(newImage))
        {
            gr.SmoothingMode = SmoothingMode.HighQuality;
            gr.InterpolationMode = InterpolationMode.HighQualityBicubic;
            gr.PixelOffsetMode = PixelOffsetMode.HighQuality;
            gr.DrawImage(bitmap, new Rectangle(0, 0, width, height));
        }

        return newImage;
    }


    internal static BitmapSource GetSourceFromBitmap(Bitmap source)
    {
        Contract.Requires(source != null);

        var ip = source.GetHbitmap();
        BitmapSource bs;
        int result;
        try
        {
            bs = Imaging.CreateBitmapSourceFromHBitmap(ip,
                IntPtr.Zero, Int32Rect.Empty,
                BitmapSizeOptions.FromEmptyOptions());
        }
        finally
        {
            result = NativeMethods.DeleteObject(ip);
        }
        if (result == 0)
            throw new InvalidOperationException("NativeMethods.DeleteObject returns 0 (operation failed)");

        return bs;
    }

    internal static class NativeMethods
    {
       [DllImport("gdi32")]
       static internal extern int DeleteObject(IntPtr o);
    }

该案例的最快和最常用的方法是什么?

1 个答案:

答案 0 :(得分:1)

我建议您使用DecodePixelWidth类的DecodePixelHeight和/或BitmapImage属性(继承自ImageSource)。

看看here

BitmapSource GetImage(byte[] dataBytes)
{
    using (MemoryStream stream = new MemoryStream(dataBytes))
    {
        BitmapImage bi = new BitmapImage();
        bi.BeginInit();

        // This is important: the image should be loaded on setting the StreamSource property, afterwards the stream will be closed
        bi.CacheOption = BitmapCacheOption.OnLoad;
        bi.DecodePixelWidth = 160;

       // Set this to 160 to get exactly 160x160 image
       // Comment it out to retain original aspect ratio having the image width 160 and auto calculated image height
       bi.DecodePixelHeight = 160;

       bi.StreamSource = stream;
       bi.EndInit();
       return bi;
    }
}