2011-09-29 75 views
0

我創建一個縮略圖,現在是這樣的:僅圖像的一部分創建縮略圖

// create thumbnail and save 
var image = Image.FromFile(Server.MapPath(imageFilename)); 
var thumb = image.GetThumbnailImage(image.Width/10, image.Height/10,() => false, IntPtr.Zero); 
thumb.Save(Server.MapPath(Path.ChangeExtension(imageFilename, "thumb" + Path.GetExtension(imageFilename)))); 

它所做的就是把圖像,生成縮略圖大小的1/10,並保存它。

我希望能夠僅從部分圖像中創建縮略圖。所以說我有一個200像素,200像素的圖像。我如何從圖像中間取出100像素×100像素的作物,並從中創建縮略圖?

回答

3

這裏裁剪圖像

http://www.bobpowell.net/changing_resolution.htm


using(Bitmap bitMap = (Bitmap)Image.FromFile("myimage.jpg")) // assumes a 400 * 300 image from which a 160 * 120 chunk will be taken 
{ 

using(Bitmap cropped = new Bitmap(160,120)) 
{ 

    using(Graphics g=Graphics.FromImage(cropped)) 
    { 

    g.DrawImage(bitMap, new Rectangle(0,0,cropped.Width,cropped.Height),100,50,cropped.Width,cropped.Height,GraphicsUnit.Pixel); 


    cropped.Save("croppedimage.jpg",ImageFormat.Jpeg); 
    } 
} 
} 
+1

沒有加不使用時退房代碼'使用()' –

+0

@ liho1eye它從另一個鏈接複製粘貼 - 目的是裁剪這裏包含的圖像代碼,沒有別的,或者我也會對命名約定發表評論。 –

+1

@ liho1eye更好嗎? :) –