繁体   English   中英

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

[英]How to resize BitmapImage using Writeable Bitmap Extension

我正在使用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;
    }

由于没有系统,我无法在Windows Hone 8或8.1中使用相同的实现。在这些平台中绘制。因此我选择了WritableBitmap扩展。它还支持Win 8,win Phone 8,8.1和WPF

过去这对我有用。 您主要感兴趣的是解码像素的宽度和高度。 ps此代码成功将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;
}

我希望这有帮助,

罗卡

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM