我想使用双三次/双二次插值将ByteArray从[5x7]调整为[241x301]像素。因此,我尝试将ByteArray转换为具有像素格式Format16bppGrayScale的Bitmap。然后,我尝试将调整大小的Bitmap转换回另一个ByteArray。
不幸的是,GDI似乎不支持格式Format16bppGrayScale的转换。我总是得到异常,例如invalid argument或out of memory。我在Google上查询了这个问题,但我只找到了这个类似的问题,它建议使用第三方库或者编写自己的代码来使用字节数组来调整大小。
有人能建议一种方法来获得一个大小调整的字节数组吗?
更新
下面的代码示例给出了一个System.ArgumentException。
static void Main(string[] args)
{
byte[] resizedBitmap = resizeImage(new byte[,] { { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 } }, 241, 301);
}
public static Byte[] resizeImage(byte[,] matrix, int width, int height)
{
// Create Bitmap from ByteArray
Bitmap original;
unsafe
{
fixed (byte* intPtr = &matrix[0, 0])
{
original = new Bitmap(5, 7, 4, PixelFormat.Format16bppGrayScale, new IntPtr(intPtr));
}
}
// Resize the Bitmap and convert it back to a ByteArray
Image newImage = new Bitmap(241, 301);
using (Graphics g = Graphics.FromImage(newImage))
{
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.Bicubic;
// Convert the Bitmap back to a ByteArray
Bitmap bmp = new Bitmap(241, 301, g);
BitmapData bmpData = bmp.LockBits(new Rectangle(new Point(), bmp.Size), ImageLockMode.ReadOnly, PixelFormat.Format16bppGrayScale);
byte[] dataAsBytes = new byte[bmpData.Stride * bmpData.Height];
System.Runtime.InteropServices.Marshal.Copy(bmpData.Scan0, dataAsBytes, 0, dataAsBytes.Length);
bmp.UnlockBits(bmpData);
return dataAsBytes;
}
}发布于 2014-07-29 11:17:39
您可以始终使用GDI+内插来执行大小调整:
Image original = new Bitmap(/* path to your 5x7 image */);
Image newImage = new Bitmap(241, 301);
using (Graphics g = Graphics.FromImage(newImage))
{
g.InterpolationMode = InterpolationMode.Bicubic;
g.DrawImage(original, new Rectangle(0, 0, newImage.Width, newImage.Height));
}https://stackoverflow.com/questions/25013840
复制相似问题