调整放在byte []数组中的图像

Fis*_*man 8 .net c#

大小图像放在byte []数组中(不知道图像的类型是什么).我必须生成其他byte []数组,其大小应该高达50kB.我该如何进行某种缩放?

Wes*_*ong 26

除非您想要进行一些严格的数学运算,否则需要将字节数组加载到内存流中,从该内存流中加载图像,并使用System.Drawing命名空间中的内置GDI函数.

做25%或50%的比例很容易.除此之外,您需要开始进行插值和差分处理,使二进制数据操作中的任何内容看起来都不错.在你可以匹配GDI中已有的内容之前,你将需要几天的时间.

System.IO.MemoryStream myMemStream = new System.IO.MemoryStream(myBytes);
System.Drawing.Image fullsizeImage = System.Drawing.Image.FromStream(myMemStream);
System.Drawing.Image newImage = fullsizeImage .GetThumbnailImage(newWidth, newHeight, null, IntPtr.Zero);
System.IO.MemoryStream myResult = new System.IO.MemoryStream();
newImage.Save(myResult ,System.Drawing.Imaging.ImageFormat.Gif);  //Or whatever format you want.
return  myResult.ToArray();  //Returns a new byte array.
Run Code Online (Sandbox Code Playgroud)

BTW - 如果您确实需要弄清楚源图像类型,请参阅:如何检查字节数组是否为有效图像


Fis*_*man 7

好的,经过一些实验,我有类似的东西:

public static byte[] Resize2Max50Kbytes(byte[] byteImageIn)
{
    byte[] currentByteImageArray = byteImageIn;
    double scale = 1f;

    if (!IsValidImage(byteImageIn))
    {
        return null;
    }

    MemoryStream inputMemoryStream = new MemoryStream(byteImageIn);
    Image fullsizeImage = Image.FromStream(inputMemoryStream);

    while (currentByteImageArray.Length > 50000)
    {
        Bitmap fullSizeBitmap = new Bitmap(fullsizeImage, new Size((int)(fullsizeImage.Width * scale), (int)(fullsizeImage.Height * scale)));
        MemoryStream resultStream = new MemoryStream();

        fullSizeBitmap.Save(resultStream, fullsizeImage.RawFormat);

        currentByteImageArray = resultStream.ToArray();
        resultStream.Dispose();
        resultStream.Close();

        scale -= 0.05f;
    }

    return currentByteImageArray;
}
Run Code Online (Sandbox Code Playgroud)

有人有另一个想法吗?不幸的是,Image.GetThumbnailImage()导致了非常脏的图像.


v01*_*1pe 0

我没有给你明确的实现,但我会这样处理:

您可以存储 51200 个值(未压缩)。并且您知道原始图像的比率:使用新图像的比率和大小计算尺寸:

x = y / ratio

size(51200) = x * y
y = size / x

x = (size / x) / ratio;
y = x * ratio
Run Code Online (Sandbox Code Playgroud)

对于值的重新采样,我将使用过滤器内核: http: //en.wikipedia.org/wiki/Lanczos_resampling

尚未使用它,但听起来很有希望。