2012-04-25 13 views
4

フォーム上のボタンを押したときに切り抜きたい画像があります。私はボタンが押されたときに実行され、次のコードを持っているが、それはイメージに何もしない:Cで画像を切り抜く#

try 
{ 
    Image image = Image.FromFile("test.jpg"); 
    Bitmap bmp = new Bitmap(200, 200, PixelFormat.Format24bppRgb); 
    bmp.SetResolution(80, 60); 

    Graphics gfx = Graphics.FromImage(bmp); 
    gfx.SmoothingMode = SmoothingMode.AntiAlias; 
    gfx.InterpolationMode = InterpolationMode.HighQualityBicubic; 
    gfx.PixelOffsetMode = PixelOffsetMode.HighQuality; 
    gfx.DrawImage(image, new Rectangle(0, 0, 200, 200), 10, 10, 200, 200, GraphicsUnit.Pixel); 
    // Dispose to free up resources 
    image.Dispose(); 
    bmp.Dispose(); 
    gfx.Dispose(); 
} 
catch (Exception ex) 
{ 
    MessageBox.Show(ex.Message); 
}  

私のイメージは、実際に次のコードをフォームのアクティブなウィンドウのスクリーンショットです:

Rectangle bounds = this.Bounds; 
using (Bitmap bitmap = new Bitmap(bounds.Width, bounds.Height)) 
{ 
    using (Graphics g = Graphics.FromImage(bitmap)) 
    { 
    g.CopyFromScreen(new Point(bounds.Left, bounds.Top), Point.Empty, bounds.Size); 
    } 
    bitmap.Save("test.jpg", ImageFormat.Jpeg); 
} 

その画像をトリミング、その後、私は、フォームのスクリーンショットを撮りたいまず、同じボタンを押すだけで、これを終了しますが、トリミングは動作しません。何故ですか?

+2

[その他の質問](http://stackoverflow.com/q/10312721/719186)にはこのコード例がありますので、削除してください。重複は投稿しないでください。 – LarsTech

答えて

3

あなたのコードは、私は、画像をトリミング保存するために使用されているものに近いです。クロップされた画像を保存する部分が欠落しています。トリミングされたイメージをバイトストリームに書き込んだ後、ディスクに保存する必要があります。私はあなたのコードを修正しました、それはテストされていませんが、試してみてください。

try 
{ 
    Image image = Image.FromFile("test.jpg"); 
    Bitmap bmp = new Bitmap(200, 200, PixelFormat.Format24bppRgb); 
    bmp.SetResolution(80, 60); 

    Graphics gfx = Graphics.FromImage(bmp); 
    gfx.SmoothingMode = SmoothingMode.AntiAlias; 
    gfx.InterpolationMode = InterpolationMode.HighQualityBicubic; 
    gfx.PixelOffsetMode = PixelOffsetMode.HighQuality; 
    gfx.DrawImage(image, new Rectangle(0, 0, 200, 200), 10, 10, 200, 200, GraphicsUnit.Pixel); 

    //Need to write the file to memory then save it 
    MemorySteam ms = new MemoryStream(); 
    bmp.Save(ms, image.RawFormat); 
    byte[] buffer = ms.GetBuffer(); 

    var stream = new MemorySteam((buffer), 0, buffer.Length); 
    var croppedImage = SD.Image.FromStream(steam, true); 
    croppedImage.Save("/your/path/image.jpg", croppedImage.RawFormat); 

    // Dispose to free up resources 
    image.Dispose(); 
    bmp.Dispose(); 
    gfx.Dispose(); 
    stream.Dispose(); 
    croppedImage.Dispose(); 

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

"var croppedImage = SD.Image.FromStream(steam、true);"で "SD" ?ありがとう。 –

0

結果は変数bmpになります。あなたはそれに取り組むことができます。そのオブジェクトを破棄すると、変更内容は失われます。

0

私はここに、私のプロジェクトのいずれかの方法を作成したメソッドは、それを使ってみて、それが動作するかどうかを確認します:

public void ResizeImage(string sImageFile, decimal dWidth, decimal dHeight, string sOutputFile) 
    { 
     Image oImg = Bitmap.FromFile(sImageFile); 
     Bitmap oBMP = new Bitmap(decimal.ToInt16(dWidth), decimal.ToInt16(dHeight)); 

     Graphics g = Graphics.FromImage(oBMP); 
     g.PageUnit = pgUnits; 
     g.SmoothingMode = psMode; 
     g.InterpolationMode = piMode; 
     g.PixelOffsetMode = ppOffsetMode; 

     g.DrawImage(oImg, 0, 0, decimal.ToInt16(dWidth), decimal.ToInt16(dHeight)); 

     ImageCodecInfo oEncoder = GetEncoder(); 
     EncoderParameters oENC = new EncoderParameters(1); 

     oENC.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, plEncoderQuality); 

     oImg.Dispose(); 

     oBMP.Save(sOutputFile, oEncoder, oENC); 
     g.Dispose(); 

    } 
関連する問題