2012-04-25 202 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 2012-04-25 13:47:25

回答

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」;「 ?。謝謝。 – 2015-04-11 07:24:50

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(); 

    }