2017-05-11 8 views
0

私はこれとしばらくの間戦っており、結果を得ることはできません。 私はこのSO QAのメソッドを使用しています>How to crop an image using C#?C#座標を使用して画像を切り取る

エラーは発生しません:/コードは実行されますが、画像は切り取られません。

コード:私は間違って何をやっていたの完全な理解を取得した後

string fileNameWitPath = "finename.png"; 
fileNameWitPath = context.Server.MapPath("~/content/branding/" + context.Request.QueryString["userId"] + "/logo" + "/" + fileNameWitPath) 

using (FileStream fs = new FileStream(fileNameWitPath, FileMode.Open)) 
{ 
    using (BinaryWriter bw = new BinaryWriter(fs)) 
    { 
     //get co-ords 
     int x1 = Convert.ToInt32(context.Request.QueryString["x1"].Trim()); 
     int y1 = Convert.ToInt32(context.Request.QueryString["y1"].Trim()); 
     int x2 = Convert.ToInt32(context.Request.QueryString["x2"].Trim()); 
     int y2 = Convert.ToInt32(context.Request.QueryString["y2"].Trim()); 

     Bitmap b = new Bitmap(fs); 
     Bitmap nb = new Bitmap((x2 - x1), (y2 - y1)); 
     Graphics g = Graphics.FromImage(nb); 
     //g.DrawImage(b, x2, y2); 
     Rectangle cropRect = new Rectangle(x1, y1, nb.Width, nb.Height); 

     g.DrawImage(b, new Rectangle(x1, y1, nb.Width, nb.Height), cropRect, GraphicsUnit.Pixel); 

     Byte[] data; 

     using (var memoryStream = new MemoryStream()) 
     { 
      nb.Save(memoryStream, ImageFormat.Png); 
      data = memoryStream.ToArray(); 
     } 

     bw.Write(data); 
     bw.Close(); 
    } 
} 
+0

まあ、コード内に少なくとも1つのタイプミスがあります。作図が終わったら( 'using'を使って)' Graphics'オブジェクトを破棄しなければなりません。そして、 'fs'ストリームに直接保存するだけでは、' BinaryWriter'と 'MemoryStream'を使う点を理解していません。 –

+0

新しいビットマップのサイズは? – kennyzx

+0

@PeterDunihoこれはタイプミスですが、それはx2 - x1を意味します。私はテストから元のコードに戻しました。結果は同じですが、 – Orion

答えて

0

。私はコードを修正しました。私は同じファイルストリームに書き込んでいたので、実際には切り取った画像を保存していませんでした。新しいファイルストリームに書き込むようにコードを変更し、切り取った画像を保存しています。

Byte[] data; 

       using (FileStream fs = new FileStream(fileNameWitPath, fileMode)) 
       { 
        using (BinaryWriter bw = new BinaryWriter(fs)) 
        { 
         //get co-ords 
         int x1 = Convert.ToInt32(context.Request.QueryString["x1"].Trim()); 
         int y1 = Convert.ToInt32(context.Request.QueryString["y1"].Trim()); 
         int x2 = Convert.ToInt32(context.Request.QueryString["x2"].Trim()); 
         int y2 = Convert.ToInt32(context.Request.QueryString["y2"].Trim()); 

         Bitmap b = new Bitmap(fs); 
         Bitmap nb = new Bitmap((x2 - x1), (y2 - y1)); 
         Graphics g = Graphics.FromImage(nb); 
         //g.DrawImage(b, x2, y2); 
         Rectangle cropRect = new Rectangle(x1, y1, nb.Width, nb.Height); 

         g.DrawImage(b, new Rectangle(0, 0, nb.Width, nb.Height), cropRect, GraphicsUnit.Pixel); 

         using (var memoryStream = new MemoryStream()) 
         { 
          nb.Save(memoryStream, ImageFormat.Png); 
          data = memoryStream.ToArray(); 
         } 

         bw.Close(); 
        } 
       } 

       using (FileStream fs = new FileStream(fileNameWitPath, fileMode)) 
       { 
        using (BinaryWriter bw = new BinaryWriter(fs)) 
        { 
         bw.Write(data); 
         bw.Close(); 
        } 
       } 
1

ます。また、それはMarshal.Copyとビットマップとの間のピクセルをコピーすることができます: `×2 = x1`:

static void Main() 
    { 
     var srcBitmap = new Bitmap(@"d:\Temp\SAE5\Resources\TestFiles\rose.jpg"); 
     var destBitmap = CropBitmap(srcBitmap, new Rectangle(10, 20, 50, 25)); 
     destBitmap.Save(@"d:\Temp\tst.png"); 
    } 

    static Bitmap CropBitmap(Bitmap sourceBitmap, Rectangle rect) 
    { 
     // Add code to check and adjust rect to be inside sourceBitmap 

     var sourceBitmapData = sourceBitmap.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppRgb); 

     var destBitmap = new Bitmap(rect.Width, rect.Height, System.Drawing.Imaging.PixelFormat.Format24bppRgb); 
     var destBitmapData = destBitmap.LockBits(new Rectangle(0, 0, rect.Width, rect.Height), System.Drawing.Imaging.ImageLockMode.WriteOnly, System.Drawing.Imaging.PixelFormat.Format32bppRgb); 

     var pixels = new int[rect.Width * rect.Height]; 
     System.Runtime.InteropServices.Marshal.Copy(sourceBitmapData.Scan0, pixels, 0, pixels.Length); 
     System.Runtime.InteropServices.Marshal.Copy(pixels, 0, destBitmapData.Scan0, pixels.Length); 

     sourceBitmap.UnlockBits(sourceBitmapData); 
     destBitmap.UnlockBits(destBitmapData); 

     return destBitmap; 
    } 
関連する問題