2011-08-14 13 views
0

これをWP7でどのように動作させるには?BitmapImageのサイズを変更するwp7

private static Bitmap ResizeBitmap(Bitmap sourceBMP, int width, int height) 
{ 
    Bitmap result = new Bitmap(width, height); 
    using (Graphics g = Graphics.FromImage(result)) 
     g.DrawImage(sourceBMP, 0, 0, width, height); 
    return result; 
} 

答えて

0

これは、私がメディアライブラリから選択した画像を縮小したり、カメラで撮影した画像です。アスペクト比を維持します。

private void PhotoChooserTaskCompleted(object sender, PhotoResult e) 
    { 
     if (e.TaskResult != TaskResult.OK) return; 

     var bmp = new BitmapImage(); 
     bmp.SetSource(e.ChosenPhoto); 

     var scaledDownImage = AspectScale(bmp, 640, 480); 

     MyImage.Source = scaledDownImage; 
    } 

    private BitmapImage AspectScale(BitmapImage img, int maxWidth, int maxHeigh) 
    { 
     double scaleRatio; 

     if (img.PixelWidth > img.PixelHeight) 
      scaleRatio = (maxWidth/(double) img.PixelWidth); 
     else 
      scaleRatio = (maxHeigh/(double) img.PixelHeight); 

     var scaledWidth = img.PixelWidth * scaleRatio; 
     var scaledHeight = img.PixelHeight * scaleRatio; 

     using (var mem = new MemoryStream()) 
     { 
      var wb = new WriteableBitmap(img); 
      wb.SaveJpeg(mem, (int)scaledWidth, (int)scaledHeight, 0, 100); 
      mem.Seek(0, SeekOrigin.Begin); 
      var bn = new BitmapImage(); 
      bn.SetSource(mem); 
      return bn; 
     } 
    } 
0

実際にあなたが望むものは正確には説明していませんが、画面上で画像を大きく/小さくしたいだけですか?またはリサイズされた画像のピクセル値にアクセスするか?

  1. 異なるサイズ/スケールにビットマップ画像をレンダリングするために、あなたの簡単な使用Image要素と、必要に応じてそのWidth/Heightを設定します。フレームワークはそれをスケーリングしてくれます。
  2. スケーリングした結果のピクセル値を取得する必要がある場合は、(1)で説明したスケーリングされたイメージからWriteableBitmapを作成します。
+0

2.このだったこれをどのように行うには? 私はメディアライブラリからの画像を持っており、ネットワークを介して送信する必要があり、画像のサイズを圧縮する必要があります(サイズを小さくする必要があります)。 – SevenDays

0

これは私があまりにも少ない512以上キロバイトと分離ストレージに画像を保存し、 注意画像サイズ縮小している方法です:Windowsの携帯電話でちょうど初心者はそうplsは

プライベートな方法の私のコードを負担しますvoid PhotoChooserTaskCompleted(オブジェクト送信者、PhotoResult e) { if(e.TaskResult!= TaskResult.OK)return;

string Imgpath = "Rafiq.jpg"; 
       SaveToIsolatedStorage(e.ChosenPhoto, Imgpath); 
} 

ここでは、このメソッドイムでこれがあなたの役に立てば幸い、分離ストレージに(画像が未満512キロバイトを減らすサイズ)e.choosenされたストリームとそのファイル名

を渡すと、リサイズした画像を保存し、

ます。private void SaveToIsolatedStorage(ストリームのImageStream、文字列filename) { (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())を使用して { を試してみてください {

   if (myIsolatedStorage.FileExists(fileName)) 
       { 
        myIsolatedStorage.DeleteFile(fileName); 
       } 
       IsolatedStorageFileStream fileStream = myIsolatedStorage.CreateFile(fileName); 

       BitmapImage bitmap = new BitmapImage(); 
       bitmap.SetSource(imageStream); 
       MessageBox.Show("ImageStream :"+ imageStream.Length.ToString()); 
       WriteableBitmap wb = new WriteableBitmap(bitmap); 

     // reeducing less than 524288 byte array 

       long compImageSize = 524288; 
       long originalsize = wb.ToByteArray().Length; 

       if ((Convert.ToInt32(originalsize)) > compImageSize) 
       { 
        WriteableBitmap wBitmap = new WriteableBitmap(bitmap); 
        int height = wBitmap.PixelHeight; 
        int width = wBitmap.PixelWidth; 



        while ((Convert.ToInt32(originalsize)) > compImageSize) 
        { 
         // wb.Resize(Convert.ToInt32(wb.PixelWidth/2), Convert.ToInt32(wb.PixelHeight/2), WriteableBitmapExtensions.Interpolation.Bilinear); 

         // data = ChangeDimension(bitmap, Convert.ToInt32(bitmap.PixelWidth/2), Convert.ToInt32(bitmap.PixelHeight/2)); 

         using (MemoryStream stream = new MemoryStream()) 
         { 
          height = Convert.ToInt32(wBitmap.PixelHeight/2); 
          width = Convert.ToInt32(wBitmap.PixelWidth/2); 

          wBitmap.SaveJpeg(stream, width, height, 0, 100); 
          stream.Seek(0, SeekOrigin.Begin); 
          wBitmap.SetSource(stream); 

         } 

         originalsize = wBitmap.ToByteArray().Length; 

        } 

        wb.SaveJpeg(fileStream, width, height, 0, 100); 
       } 
       else 
       { 
        wb.SaveJpeg(fileStream, wb.PixelWidth, wb.PixelHeight, 0, 100); 
       } 

       fileStream.Close();   


      } 
     } 
     catch (Exception ex) 
     { 
      MessageBox.Show(ex.Message); 
     } 
    } 

希望参考になっ

関連する問題