调整放置在byte []数组中的图像的大小 [英] resize image which is placed in byte[] array

查看:89
本文介绍了调整放置在byte []数组中的图像的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

size图像(不知道图像的类型是什么).我必须产生另一个byte []数组,该数组的大小最大为50kB.如何进行某种缩放?

size image which is placed in byte[] array (don't know what is the type of image). I have to produce other byte[] array, which size should be up to 50kB. How can I do some kind scaling?

推荐答案

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

Unless you want to get into some serious math, you need to load your byte array into a memory stream, load an image from that memory stream, and use the built-in GDI functions in the System.Drawing namespace.

进行25%或50%的缩放很容易.除此之外,您需要开始进行插值和求差,以使任何事物在二进制数据处理中看起来都像样.您需要花几天的时间才能匹配GDI中已提供的功能.

Doing a 25%, or 50% scale is easy. Beyond that, you need to start doing interpolation and differencing to make anything look halfway decent in binary data manipulation. You'll be several days into it before you can match what's already available in 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.

顺便说一句-如果您确实需要确定源图像的类型,请参见:

BTW - if you really need to figure out your source image type, see: How to check if a byte array is a valid image

这篇关于调整放置在byte []数组中的图像的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆