2017-01-06 13 views
-3

私はこの写真を持っています(この画像はX:467 Y:300です)私はXから必要があります:0 Y:0 X:200 Y :45私は、作物のためにこのコードを使用しかし、私はXデータをロスト:0、Y:0をX:200 Y:45に私はあなたがしたいことを理解し、コメントから画像のXとYを設定するには画像を保存するにはC#

bmp.Save("@image.png", ImageFormat.Png); 
     Image img = new Bitmap("@image.png"); 
     Rectangle source = new Rectangle(0, 0, ww, hh); 
     Image cropped = CropImage(img, source); 
     cropped.Save(Path.GetDirectoryName("@image.png") + "croppped" + Path.GetExtension("@image.png")); 
    } 

    private Bitmap CropImage(Image originalImage, Rectangle sourceRectangle,Rectangle? destinationRectangle = null) 
    { 
     if (destinationRectangle == null) 
     { 
      destinationRectangle = new Rectangle(Point.Empty, sourceRectangle.Size); 
     } 

     var croppedImage = new Bitmap(destinationRectangle.Value.Width, 
      destinationRectangle.Value.Height); 
     using (var graphics = Graphics.FromImage(croppedImage)) 
     { 
      graphics.DrawImage(originalImage, destinationRectangle.Value, 
       sourceRectangle, GraphicsUnit.Pixel); 
     } 
     return croppedImage; 
    } 
+0

「CropImage」はうまく機能しているようです。これは実際のコードですか、あるいは矩形のパラメータに異なる値を指定していますか? –

+0

@ C.Evenhuisはいこれが私のオリジナルコードですオリジナルイメージを使わずにイメージを作る方法はありますか? – amir

+0

@ C.Evenhuisその働きはあるものの、データを失ったいくつかhow – amir

答えて

0

を何をすべきかわからないでください元のイメージをディスクに保存する前にイメージをトリミングします。その場合は、直接クロップメソッドに画像を供給することができます:

// bmp.Save("@image.png", ImageFormat.Png); 
    // Image img = new Bitmap("@image.png"); 
    Rectangle source = new Rectangle(0, 0, ww, hh); 
    Image cropped = CropImage(bmp, source); // <-- bmp supplied to crop method directly 
    cropped.Save(Path.GetDirectoryName("@image.png") + "croppped" + Path.GetExtension("@image.png")); 
関連する問題