如何使用 Writeable Bitmap Extension 调整 BitmapImage 的大小

How to resize BitmapImage using Writeable Bitmap Extension

我正在使用 WriteableBitmap 扩展 Available here 用于 BitMapImage 相关 operations.I 正在开发 windows phone 8 和 8.1 应用程序,我正在尝试从中加载一些图像压缩文件。我需要在加载图像时根据设备的屏幕尺寸调整图像大小。

  1. 我如何使用 WritableBitMapEx 实现此目的?我找不到任何 该框架的文档。
  2. 或者如果没有 WritableBitMapEx 我该如何实现?

注:

我正在开发一个支持 WPF 的跨平台移动应用程序,Windows Phone 8,Windows Phone 8.1.I 实现了相同的 senario在 WPF 中使用以下代码。

    private Bitmap LoadAndResizeBitmap()
    {
        Bitmap bitmap = (Bitmap)Bitmap.FromStream(fileEntry.OpenEntryStream());

        if (bitmap.Height > _primaryscreenHeight)
        {
            System.Drawing.Size oldSize = new System.Drawing.Size(bitmap.Width, bitmap.Height);
            System.Drawing.Size newSize = GetNewImageSize(oldSize);
            Bitmap newImage = ResizeImage(bitmap, newSize);
            bitmap = newImage;
        }
        return bitmap;
    }


    /// <summary>


    /// <summary>
    /// Resize the Image
    /// </summary>
    /// <param name="image"></param>
    /// <param name="newSize"></param>
    /// <returns></returns>
    private Bitmap ResizeImage(Image image, System.Drawing.Size newSize)
    {
        // Make a rectangle that is the new size
        Rectangle destRect = new Rectangle(0, 0, newSize.Width, newSize.Height);

        // Make a bitmap that is the new size
        Bitmap destImage = new Bitmap(newSize.Width, newSize.Height);

        // Set new image to the resolution of the original
        destImage.SetResolution(image.HorizontalResolution, image.VerticalResolution);

        // Create a GDI holder and use it
        using (Graphics graphics = Graphics.FromImage(destImage))
        {
            // Set our quality options
            graphics.CompositingMode = CompositingMode.SourceCopy;
            graphics.CompositingQuality = CompositingQuality.HighQuality;
            graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
            graphics.SmoothingMode = SmoothingMode.HighQuality;
            graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;

            // Resize original image into new one
            using (var wrapMode = new ImageAttributes())
            {
                wrapMode.SetWrapMode(WrapMode.TileFlipXY);
                graphics.DrawImage(image, destRect, 0, 0, image.Width, image.Height, GraphicsUnit.Pixel, wrapMode);
            }
        }
        return destImage;
    }

    /// <summary>
    /// Get new Image Size
    /// </summary>
    /// <param name="oldSize"></param>
    /// <returns></returns>
    private System.Drawing.Size GetNewImageSize(System.Drawing.Size oldSize)
    {
        float ratio;
        if (oldSize.Height > oldSize.Width)
        {
            ratio = (float) oldSize.Width/oldSize.Height;
        }
        else
        {
            ratio = (float) oldSize.Height/oldSize.Width;
        }
        int newWidth = (int) (_maxImageHeight*ratio);
        System.Drawing.Size newSize = new System.Drawing.Size(newWidth, _maxImageHeight);
        return newSize;
    }

我不能在 windows hone 8 或 8.1 中使用相同的实现,因为我们在这些 system.Drawing 中没有 platfoms.Thus 我选择了 WritableBitmap extension.Beside 它支持 Win 8,赢 Phone 8,8.1 和 WPF

这在过去对我有用。您主要感兴趣的是解码像素宽度和高度。 P.s。此代码成功地将 byte[](数据库友好)转换为 BitmapImage:

using(MemoryStream strmImg = new MemoryStream(profileImage.Image))
{
    BitmapImage myBitmapImage = new BitmapImage();               
    myBitmapImage.BeginInit();
    myBitmapImage.CacheOption = BitmapCacheOption.OnLoad;
    myBitmapImage.StreamSource = strmImg;
    myBitmapImage.DecodePixelWidth = 200;
    myBitmapImage.DecodePixelHeight= 250;
    myBitmapImage.EndInit();
    EmployeeProfileImage = myBitmapImage;
}

希望对您有所帮助,

罗卡