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

时间:2015-11-05 20:14:26

标签: c# wpf image-processing windows-phone-8 bitmap

我正在使用WriteableBitmap扩展Available here进行BitMapImage相关操作。我正在开发一个Windows Phone 8和8.1应用程序,我试图从Zipfile加载一些图像。我需要在加载图像时根据设备的屏幕尺寸调整图像大小。

  1. 如何使用WritableBitMapEx实现这一目标?我找不到任何东西 该框架的文档。
  2. 或者我如何在没有WritableBitMapEx的情况下实现这一目标?
  3. 注意:

    我正在开发支持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中使用相同的实现,因为我们没有system.Drawing在这些platfoms.Thus我选择了WritableBitmap扩展。除了它支持Win 8,赢得Phone 8,8.1和WPF

1 个答案:

答案 0 :(得分:1)

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

我希望这有帮助,

ROKA