如何使用可写位图扩展调整BitmapImage的大小

本文关键字:BitmapImage 调整 扩展 何使用 位图 | 更新日期: 2023-09-27 18:20:55

我正在使用WriteableBitmap扩展。此处可用于BitMapImage相关操作。我正在开发一个windows phone 8和8.1应用程序,试图从Zipfile加载一些图像。当我加载图像时,我需要根据设备的屏幕大小调整图像大小。

  1. 如何使用WritableBitMapEx实现这一点?我找不到该框架的文档
  2. 或者,如果没有WritableBitMapEx,我如何实现这一点

注:

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

    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;
    }

我不能在windowshone8或8.1中使用相同的实现,因为我们没有系统。在这些平台上画画。因此,我选择了WritableBitmap扩展。除此之外,它还支持Win 8、Win Phone 8,8.1和WPF

如何使用可写位图扩展调整BitmapImage的大小

这在过去对我很有效。你感兴趣的主要是解码像素的宽度和高度。附言:此代码成功地将字节[](数据库友好)转换为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;
}

我希望这能有所帮助,

Roka