首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >调整ByteArray或GrayScale图像的大小

调整ByteArray或GrayScale图像的大小
EN

Stack Overflow用户
提问于 2014-07-29 11:05:53
回答 1查看 839关注 0票数 1

我想使用双三次/双二次插值将ByteArray[5x7]调整为[241x301]像素。因此,我尝试将ByteArray转换为具有像素格式Format16bppGrayScaleBitmap。然后,我尝试将调整大小的Bitmap转换回另一个ByteArray

不幸的是,GDI似乎不支持格式Format16bppGrayScale的转换。我总是得到异常,例如invalid argumentout of memory。我在Google上查询了这个问题,但我只找到了这个类似的问题,它建议使用第三方库或者编写自己的代码来使用字节数组来调整大小。

有人能建议一种方法来获得一个大小调整的字节数组吗?

更新

下面的代码示例给出了一个System.ArgumentException

代码语言:javascript
复制
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;
    }       
}
EN

回答 1

Stack Overflow用户

发布于 2014-07-29 11:17:39

您可以始终使用GDI+内插来执行大小调整:

代码语言:javascript
复制
        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));
        }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25013840

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档