2014-10-29 86 views
0

我有一張類型爲byte[]的照片。我在一個datagridview單元格中設置了這張圖片。我現在可以看到它,但它太大了。我想在此單元格中調整此圖片的大小。我怎樣才能做到這一點?如何調整字節數組中的圖片大小?

這是我創建列,並設置字節數組圖片代碼:

// player picture column 
string propPlayerPicture = "PlayerPicture"; 
table.Columns.Add(propPlayerPicture, typeof(byte[])); 

// set playerPicture, noted that GetPlayerPictureAsync returns a byte array 
row[propPlayerPicture] = await GetPlayerPictureAsync(auctionInfo); 

回答

1

可以字節數組首先轉換爲Image,並將其調整到合適的大小,將其設置爲前datagridview單元格。

int maxwidth = 100; 
int maxheight = 100; 

//convert to full size image 
ImageConverter ic = new ImageConverter(); 
Image img = (Image)(ic.ConvertFrom(bytearray)); //original size 
if (img.Width > maxwidth | img.Height > maxheight) //resize if it is too big 
{ 
    Bitmap bitmap = new Bitmap(maxwidth, maxheight); 
    using (Graphics graphics = Graphics.FromImage((Image)bitmap)) 
     graphics.DrawImage(img, 0, 0, maxwidth, maxheight); 
    img = bitmap; 
} 

然後

row[propPlayerPicture] = img; 
+0

由於它的工作原理.. – Ola 2014-10-31 12:56:22