この画像はリサイズコード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が私が必要としているものであるかどうかはわかりませんが、私はそれを実行することができませんが、画像が水平か垂直かどうかを検出するだけです。垂直の場合は、それが私の指定された領域に収まるように(それは大きくても小さくても)サイズを変更します。同様に、水平は垂直と同じ方法です。
ビットマップのアスペクト比が*ポートレート*(つまり、ワイドよりも大きい場合)を* landscape *に変換したいが、回転させたくない場合。どうしたらいいの?パーツを切ったり、歪ませたり(例えば、一方向のスケーリングのみ)? BitmapImageはImage要素によって確実に表示されます。Image要素は、Sourceを均等に伸ばすことができるので、BitmapImageをまったく操作する必要はありません。 – Clemens
[WPFでBitMapImageのサイズを変更すると、どのようなオブジェクトを要素に入れることができますか?](https://stackoverflow.com/questions/17072775/changing-the-dimensions-of-a) -bitmapimage-in-wpf-what-kind-of-objects-can-i) –
PaulF
私はあなたがこのコードをすべて駄目にして、イメージの 'Stretch'属性を使うと思う。 –