如何从字节数组调整图片大小?

时间:2014-10-29 14:15:52

标签: image winforms datagridview bytearray

我有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 个答案:

答案 0 :(得分: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;
相关问题