2011-01-13 15 views
5

この私がイメージHOWTOスケールを知っているが、私はキャンバスのサイズを拡大しないか、まだ要求された画像を確認するためにSave image to file keeping aspect ration in a WPF app展開キャンバス/ WPFアプリでのBitmapImageで透明な背景

のフォローアップの質問です幅と高さ。この例では250x250ですが、動的です。

私が共犯しようとしていることを示すために、このイラストレーションを作成しました。 alt text

私は透明な背景を持つ、のBitmapImageのキャンバスを拡大する任意の方法、また正しいサイズのメモリイメージで作成する方法を見つけ、その後、2枚の画像を一緒にマージすることはできません。

答えて

6

CroppedBitmapは画像の周りにスペースを追加することをサポートしていないように思われるので、透明な画像をWriteableBitmapを使って正しいサイズで作成することができます。入力がターゲットサイズより小さい場合、このメソッドはそれを拡大しますが、変更するのは簡単です。

public static BitmapSource FitImage(BitmapSource input, int width, int height) 
{ 
    if (input.PixelWidth == width && input.PixelHeight == height) 
     return input; 

    if(input.Format != PixelFormats.Bgra32 || input.Format != PixelFormats.Pbgra32) 
     input = new FormatConvertedBitmap(input, PixelFormats.Bgra32, null, 0); 

    //Use the same scale for x and y to keep aspect ratio. 
    double scale = Math.Min((double)width/input.PixelWidth, height/(double)input.PixelHeight); 

    int x = (int)Math.Round((width - (input.PixelWidth * scale))/2); 
    int y = (int)Math.Round((height - (input.PixelHeight * scale))/2); 


    var scaled = new TransformedBitmap(input, new ScaleTransform(scale, scale)); 
    var stride = scaled.PixelWidth * (scaled.Format.BitsPerPixel/8); 

    var result = new WriteableBitmap(width, height, input.DpiX, input.DpiY, input.Format,null); 

    var data = new byte[scaled.PixelHeight * stride]; 
    scaled.CopyPixels(data, stride, 0); 
    result.WritePixels(new Int32Rect(0,0,scaled.PixelWidth,scaled.PixelHeight), data, stride,x,y); 
    return result; 
} 

あなたはすでにあなたがスケーリングを行うためにViewBoxをして、それをラップでしたが、あなただけの通常の画像で作業している場合、私は上記の方法を使用したいRenderTargetBitmap使用してコンテンツをレンダリングしている場合。

+0

@Krisこれは非常に有望ですね。私はこれらのメソッドに新しいです。 BitmapSourceとBitmapFrameの違いを教えてください。 BitmapFrameを使用して画像のサイズを変更しています。 – mrtsherman

+0

@Kris大丈夫です。BitmapFrameはBitmapSourceの子です。だから、コードを動作させることができるはずです。今自分を試している。 – mrtsherman

+0

BitmapFrameの主な用途は、gifのようないくつかの画像フォーマットは複数のフレームを持つことができるため、デコーダに結びつけてサムネイルを提供するためです。詳細については、http://stackoverflow.com/questions/155391/difference-between- a-bitmapframe-and-bitmapimage-in-wpf – Kris

0

StretchプロパティをUniformに設定する必要があります。

+0

しかし、画像を保存しても機能しません。 – gulbaek

+0

キャンバスから画像を保存するのはどうですか? –

+0

これを説明する小さな例を作成できますか?以前の質問でもわかるように、私はキャンバスを使用していませんが、イメージをBitmapImageにロードし、TransformedBitmapにロードしてサイズを縮小しますが、正しいサイズの出力は得られません。 – gulbaek

0

他人がフォームアップロードからファイルを後処理しようとしている場合のちょっとしたコードです。

if (file.PostedFile != null) 
{ 
    //write the new file to disk 
    string cachePath = String.Format("{0}temp\\", Request.PhysicalApplicationPath); 
    string photoPath = String.Format("{0}temp.png", cachePath);     
    if (!Directory.Exists(cachePath)) 
{ 
    Directory.CreateDirectory(cachePath); 
}  

file.PostedFile.SaveAs(photoPath); 

//resize the new file and save it to disk    
BitmapSource banana = FitImage(ReadBitmapFrame(file.PostedFile.InputStream), 640, 480); 
PngBitmapEncoder encoder = new PngBitmapEncoder(); 
encoder.Frames.Add(BitmapFrame.Create(banana)); 
FileStream pngStream = new FileStream(cachePath + "test.png", FileMode.Create); 
encoder.Save(pngStream); 
pngStream.Close(); 

//set a couple images on page to the newly uploaded and newly processed files 
image.Src = "temp/temp.png"; 
image.Visible = true; 
image2.Src = "temp/test.png"; 


    image2.Visible = true; 
}