2017-05-11 110 views
0

我正在與此爭鬥一段時間,似乎無法得到任何結果。 我使用的方法從此質量保證>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

嗯,有在代碼中的至少一個錯字:'X2 = x1'。繪製完成後(使用''''),你也應該放置你的'Graphics'對象。我不明白使用'BinaryWriter'和'MemoryStream'的意義,因爲你可以直接保存到'fs'流中。 –

+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:

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; 
    }