2017-12-19 17 views
0

この画像はリサイズコードhereで、私は現在のコードに適応しようとしています。C#WPF画像の縦横比

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コードが画像を使用していますし、私の現在のコードは、のBitmapImageを使用していることです。

私はどちらか一方の形式またはもう一方の形式に変換する方法を見つけようとしていましたが、その方法ではうまくいかないようです。

resizeImageが私が必要としているものであるかどうかはわかりませんが、私はそれを実行することができませんが、画像が水平か垂直かどうかを検出するだけです。垂直の場合は、それが私の指定された領域に収まるように(それは大きくても小さくても)サイズを変更します。同様に、水平は垂直と同じ方法です。

+0

ビットマップのアスペクト比が*ポートレート*(つまり、ワイドよりも大きい場合)を* landscape *に変換したいが、回転させたくない場合。どうしたらいいの?パーツを切ったり、歪ませたり(例えば、一方向のスケーリングのみ)? BitmapImageはImage要素によって確実に表示されます。Image要素は、Sourceを均等に伸ばすことができるので、BitmapImageをまったく操作する必要はありません。 – Clemens

+0

[WPFでBitMapImageのサイズを変更すると、どのようなオブジェクトを要素に入れることができますか?](https://stackoverflow.com/questions/17072775/changing-the-dimensions-of-a) -bitmapimage-in-wpf-what-kind-of-objects-can-i) – PaulF

+1

私はあなたがこのコードをすべて駄目にして、イメージの 'Stretch'属性を使うと思う。 –

答えて

0

は、サイズを変換する多くの方法がありますが、私は System.Drawing.BitmapにからしたBitmapSource の変換についてウェブ上あまりありませんので、直接あなたの質問に答えることを望んでいました。 (他の方法については多くの記事があります)。

この関数は、任意のWPFたBitmapSourceを変換する - たBitmapSourceでのBitmapImage、を含む - 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); 
関連する問題