第一篇博客
###################################################################
小菜鸟咸鱼咸鱼一波
######################################################################
随便做了一个类似订餐系统 ,局域网内使用
想服务器有食物的照片通过字节直接和客户端交互
但是这样图片如果太大就不行了
所以就试试图片压缩
本文记录压缩时遇到的问题
########################################################################
填几个坑
1. unity 添加 System.Drawing 引用 (http://www.manew.com/thread-44136-1-1.html)
- [在Unity的安装路径中找到System.Drawing.dll,将其复制到我们的项目文件夹]
- [
System.Drawing.dll的具体位置:%Unity根目录%\Editor\Data\Mono\lib\mono\2.0\System.Drawing.dll ]
- [在Unity编辑器界面打开Player Settings面板->Other Settings,将Api Compatibility Level 从 ".NET 2.0 Subset" 改为 ".NET 2.0"]
2.在保存压缩图片时出现的错误
- [System.InvalidOperationException: The operation is invalid [GDI+ status: Win32Error]
]
++查找文档(https://msdn.microsoft.com/zh-cn/library/ytz20d80(v=vs.80).aspx)发现,备注里写的:不允许将图像保存到构造该图像的文件,这样会引发异常。++
++我原本是把图片存储在Application.streamingAssetsPath +“/Icon”路径不存在,所以会报错 试着将图片存在c盘就可以了++
脚本[(参考)](http://blog.youkuaiyun.com/qq_16542775/article/details/51792149)
/// <summary>
/// 无损压缩图片
/// </summary>
/// <param name="sFile">原图片</param>
/// <param name="dFile">压缩后保存位置</param>
/// <param name="dHeight">高度</param>
/// <param name="dWidth"></param>
/// <param name="flag">压缩质量(数字越小压缩率越高) 1-100</param>
/// <returns></returns>
public static bool GetPicThumbnail(string sFile, string dFile,/*int dHeight, int dWidth,*/ int flag)
{
System.Drawing.Image iSource = System.Drawing.Image.FromFile(sFile);
ImageFormat tFormat = iSource.RawFormat;
int sW = 0, sH = 0;
//按比例缩放
Size tem_size = new Size(iSource.Width, iSource.Height);
int dHeight = iSource.Height;//不进行比例缩放
int dWidth = iSource.Width;
//if (tem_size.width > dheight || tem_size.width > dwidth)
//{
// if ((tem_size.width * dheight) > (tem_size.width * dwidth))
// {
// sw = dwidth;
// sh = (dwidth * tem_size.height) / tem_size.width;
// }
// else
// {
// sh = dheight;
// sw = (tem_size.width * dheight) / tem_size.height;
// }
//}
//else
//{
sW = tem_size.Width;
sH = tem_size.Height;
//}
//int dWidth = sW;
//int dHeight = sH;
Bitmap ob = new Bitmap(dWidth, dHeight);
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(ob);
g.Clear(System.Drawing.Color.WhiteSmoke);
g.CompositingQuality = CompositingQuality.HighQuality;
g.SmoothingMode = SmoothingMode.HighQuality;
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.DrawImage(iSource, new Rectangle((dWidth - sW) / 2, (dHeight - sH) / 2, sW, sH), 0, 0, iSource.Width, iSource.Height, GraphicsUnit.Pixel);
g.Dispose();
//以下代码为保存图片时,设置压缩质量
EncoderParameters ep = new EncoderParameters();
long[] qy = new long[1];
qy[0] = flag;//设置压缩的比例1-100
EncoderParameter eParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, qy);
ep.Param[0] = eParam;
try
{
ImageCodecInfo[] arrayICI = ImageCodecInfo.GetImageEncoders();
ImageCodecInfo jpegICIinfo = null;
for (int x = 0; x < arrayICI.Length; x++)
{
if (arrayICI[x].FormatDescription.Equals("JPEG"))
{
jpegICIinfo = arrayICI[x];
break;
}
}
if (jpegICIinfo != null)
{
//这里有坑
ob.Save(dFile, jpegICIinfo, ep);//dFile是压缩后的新路径
}
else
{
ob.Save(dFile, tFormat);//这里有坑
}
return true;
}
catch(Exception ex)
{
Debug.LogError(ex);
return false;
}
finally
{
iSource.Dispose();
ob.Dispose();
}
}
最后加上自己图片字节转换 = =。
/// <summary>
/// 字节流转换成图片
/// </summary>
public static Texture2D BytToImg(byte[] byt)
{
Texture2D img = new Texture2D(140, 140);
img.LoadImage(byt);
return img;
}
//
/// <summary>
/// 根据图片路径返回图片的字节流byte[]
/// </summary>
/// <param name="imagePath">图片路径</param>
/// <returns>返回的字节流</returns>
public static byte[] GetImageByte(Texture2D imag)
{
byte[] imgByte = ((Texture2D)imag).EncodeToPNG();
Debug.LogFormat("根据图片路径返回图片的字节流byte:{0}", imgByte.Length);
return imgByte;
}