2014-10-29 43 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