C#WPF图片比率垂直和/或水平

时间:2017-12-19 14:57:39

标签: c# wpf bitmapimage

嘿所有我发现此图片调整了代码here,我正在尝试将其调整为我当前的代码。

private void resizeImage(string path, string originalFilename, 
                     /* note changed names */
                     int canvasWidth, int canvasHeight, 
                     /* new */
                     int originalWidth, int originalHeight)
{
    Image image = Image.FromFile(path + originalFilename);

    System.Drawing.Image thumbnail = 
        new Bitmap(canvasWidth, canvasHeight); // changed parm names
    System.Drawing.Graphics graphic = 
                 System.Drawing.Graphics.FromImage(thumbnail);

    graphic.InterpolationMode = InterpolationMode.HighQualityBicubic;
    graphic.SmoothingMode = SmoothingMode.HighQuality;
    graphic.PixelOffsetMode = PixelOffsetMode.HighQuality;
    graphic.CompositingQuality = CompositingQuality.HighQuality;

    /* ------------------ new code --------------- */

    // Figure out the ratio
    double ratioX = (double) canvasWidth / (double) originalWidth;
    double ratioY = (double) canvasHeight / (double) originalHeight;
    // use whichever multiplier is smaller
    double ratio = ratioX < ratioY ? ratioX : ratioY;

    // now we can get the new height and width
    int newHeight = Convert.ToInt32(originalHeight * ratio);
    int newWidth = Convert.ToInt32(originalWidth * ratio);

    // Now calculate the X,Y position of the upper-left corner 
    // (one of these will always be zero)
    int posX = Convert.ToInt32((canvasWidth - (originalWidth * ratio)) / 2);
    int posY = Convert.ToInt32((canvasHeight - (originalHeight * ratio)) / 2);

    graphic.Clear(Color.White); // white padding
    graphic.DrawImage(image, posX, posY, newWidth, newHeight);

    /* ------------- end new code ---------------- */

    System.Drawing.Imaging.ImageCodecInfo[] info =
                     ImageCodecInfo.GetImageEncoders();
    EncoderParameters encoderParameters;
    encoderParameters = new EncoderParameters(1);
    encoderParameters.Param[0] = new EncoderParameter(Encoder.Quality,
                     100L);            
    thumbnail.Save(path + newWidth + "." + originalFilename, info[1], 
                     encoderParameters);
}

我目前的代码:

public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
    string path = value as string;

    if (path != null) {
       //create new stream and create bitmap frame
       var bitmapImage = new BitmapImage();

       bitmapImage.BeginInit();
       bitmapImage.StreamSource = new FileStream(path, FileMode.Open, FileAccess.Read);
       //load the image now so we can immediately dispose of the stream
       bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
       bitmapImage.EndInit();

       //clean up the stream to avoid file access exceptions when attempting to delete images
       bitmapImage.StreamSource.Dispose();

       return bitmapImage;
    } else {
       return DependencyProperty.UnsetValue;
    }
}

问题在于resizeImage代码使用 Image ,而我当前的代码使用 BitmapImage

我一直试图找到转换/或转换为一种格式的方法,但我似乎没有成功。

不确定 resizeImage 是否是我需要的,因为我无法按原样运行它,但我想要做的就是检测图像是水平还是垂直。如果它的垂直然后调整大小(要么更大或更小),以便它适合我指定的区域。同样,水平方向与垂直方向相同。

1 个答案:

答案 0 :(得分:-1)

有很多方法可以转换尺寸,但我想直接回答您的问题,因为网上关于将 BitmapSource 转换为的内容并不多一个System.Drawing.Bitmap。 (有很多文章在另一方面)。

此函数会将任何WPF BitmapSource(包括BitmapIource,BitmapSource)转换为System.Drawing.Bitmap:

private System.Drawing.Bitmap ConvertBitmapSourceToDrawingBitmap(BitmapSource image)
{
    int width = Convert.ToInt32(image.Width);
    int height = Convert.ToInt32(image.Height);
    int stride = Convert.ToInt32(width * ((image.Format.BitsPerPixel + 7) / 8) + 0.5);

    byte[] bits = new byte[height * stride];
    image.CopyPixels(bits, stride, 0);

    System.Drawing.Bitmap bitmap = null;
    unsafe
    {
        fixed (byte* pBits = bits)
        {
            IntPtr ptr = new IntPtr(pBits);
            bitmap = new System.Drawing.Bitmap(width, height, stride, System.Drawing.Imaging.PixelFormat.Format32bppPArgb, ptr);
        }
    }

    return bitmap;
}

为了测试它,我使用了这段代码:

    System.Windows.Media.Imaging.BitmapSource bitmapSource;
    using (System.IO.Stream stm = System.IO.File.Open("c:\\foo_in.png", System.IO.FileMode.Open, System.IO.FileAccess.Read))
    {
        // Since we're not specifying a System.Windows.Media.Imaging.BitmapCacheOption, the pixel format
        // will be System.Windows.Media.PixelFormats.Pbgra32.
        bitmapSource = System.Windows.Media.Imaging.BitmapFrame.Create(
            stm,
            System.Windows.Media.Imaging.BitmapCreateOptions.None,
            System.Windows.Media.Imaging.BitmapCacheOption.OnLoad);
    }
    System.Drawing.Bitmap bmp = ConvertBitmapSourceToDrawingBitmap(bitmapSource);
    bmp.Save("c:\\foo_out.png", System.Drawing.Imaging.ImageFormat.Png);