2017-05-11 6 views
0

Ich kämpfe jetzt schon seit einiger Zeit und kann anscheinend keine Ergebnisse erzielen. Ich verwende Methoden aus diesem SO QA>How to crop an image using C#?C# Bild zuschneiden mit Koordinaten

Ich bekomme keine Fehler:/Der Code läuft nur, aber das Bild wird nicht abgeschnitten.

Code:

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

Nun, es gibt mindestens einen Tippfehler in dem Code: 'x2 = x1'. Sie sollten auch Ihr 'Graphics'-Objekt entsor- gen, nachdem Sie mit dem Zeichnen fertig sind (mit' using'). Und ich verstehe nicht den Punkt der Verwendung von 'BinaryWriter' und einem' MemoryStream', wenn Sie einfach direkt in den 'fs' Stream speichern können. –

+0

Wie groß ist die neue Bitmap? – kennyzx

+0

@PeterDuniho das ist ein Tippfehler, es soll x2 - x1 sein. Ich habe den Code von meinen Tests wieder auf das Original zurückgesetzt. Ergebnis ist das gleiche obwohl – Orion

Antwort

0

Nach einem vollen Verständnis zu bekommen, was ich falsch machen. Ich habe dann den Code geändert. Ich schrieb in den gleichen Dateistream, sodass das zugeschnittene Bild nicht gespeichert wurde. Der Code wurde geändert, um in einen neuen Dateistream zu schreiben und das zugeschnittene Bild wird jetzt gespeichert.

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

Sie können auch Sie kopieren es Pixel zwischen Bitmaps mit 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; 
    }