好难的一个问题,高手近来看看。 问题是这样的,我有一个byte数组,里面保存了一个图片文件的信息。我不想创建新的文件。我现在想缩小这个图片到一定比例,然后得到一个新byte的数组。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 先通过数据在内在中生成一张图片,然后调整大小,然后保存到Stream然后读回bytes byte[] oldByte = new byte[100];//原始二进制字节流System.IO.MemoryStream ms = new System.IO.MemoryStream(oldByte);System.Drawing.Image img1 = System.Drawing.Image.FromStream(ms);System.Drawing.Image.GetThumbnailImageAbort myCallback = new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback);System.Drawing.Image img2 = tmp1.GetThumbnailImage(tmp1.Width/2,tmp1.Height/2,myCallback, IntPtr.Zero);//楼主在此调整比例ms = new System.IO.MemoryStream();img2.Save(ms,System.Drawing.Imaging.ImageFormat.Jpeg);byte[] newByte = new byte[ms.Length];ms.Read(newByte,o,ms.Length);ms.Close();img1.Dispose();img2.Dispose();//得到newByte 我不想创建新的文件。=============用MemoryStream public static byte[] Test(byte[] oldByte){ System.IO.MemoryStream ms = new System.IO.MemoryStream(oldByte); System.Drawing.Image img1 = System.Drawing.Image.FromStream(ms); System.Drawing.Image.GetThumbnailImageAbort myCallback = new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback); System.Drawing.Image img2 = tmp1.GetThumbnailImage(tmp1.Width/2,tmp1.Height/2,myCallback, IntPtr.Zero);//楼主在此调整比例 ms = new System.IO.MemoryStream(); img2.Save(ms,System.Drawing.Imaging.ImageFormat.Jpeg); byte[] newByte = new byte[ms.Length]; ms.Read(newByte,o,ms.Length); ms.Close(); img1.Dispose(); img2.Dispose(); return newByte;//得到newByte}public static bool ThumbnailCallback(){ return false;} 很久以前我做过同样的尝试就是用MemoryStream但遇到与楼主同样的问题后来放弃了也许是水平不够关注一下~ 刚才的代码确有问题,sorry~ 更正如下:public static byte[] Test(byte[] oldByte){ System.IO.MemoryStream ms = new System.IO.MemoryStream(oldByte); System.Drawing.Image img1 = System.Drawing.Image.FromStream(ms); System.Drawing.Image.GetThumbnailImageAbort myCallback = new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback); System.Drawing.Image img2 = img1.GetThumbnailImage(img1.Width/2,img1.Height/2,myCallback, IntPtr.Zero);//楼主在此调整比例 ms = new System.IO.MemoryStream(); img2.Save(ms,System.Drawing.Imaging.ImageFormat.Jpeg); byte[] newByte = new byte[ms.Length]; ms.Position=0;//漏了这个 ms.Read(newByte,0,newByte.Length); ms.Close(); img1.Dispose(); img2.Dispose(); return newByte;//得到newByte}public static bool ThumbnailCallback(){ return false;} 终于搞定了,呵呵。Eddie005(♂) №.零零伍 (♂) 少给了一行代码。 byte[] newByte = new byte[ms.Length]; //在这里插入下面的语句。 ms.Read(newByte,o,ms.Length); ms.Close(); img1.Dispose(); img2.Dispose();=================================================ms.Seek(0, SeekOrigin.Begin);原因可能是ms = new System.IO.MemoryStream(); img2.Save(ms,System.Drawing.Imaging.ImageFormat.Jpeg);填充ms流的时候会把流的位置的指针移到了结尾。所以读的时候就返回空内容了。 girdview嵌套dropdownlist问题 有没有人知道那些提供免费发短信的网站,发短信的功能是怎么做的? 请教一个弱弱的问题。 关于大型网站的整站搜索,有什么解决方案?大家都是怎么做的? 时间格式的转换 [简单问题]Request.From无法取到汉字. asp.net安装部署出现的离奇问题!老手进! smart client 初级小问题 关于如果禁止使用视图状态后datagrid控件怎么删除数据呢? a是一个web控件,b是一个服务器端运行的html控件,用什么方法在a里取得b的事件(如点击)? ASPNET(C#)中远程调用WebService问题? 动态生成按钮的问题?
System.Drawing.Image img1 = System.Drawing.Image.FromStream(ms);
System.Drawing.Image.GetThumbnailImageAbort myCallback = new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback);
System.Drawing.Image img2 = tmp1.GetThumbnailImage(tmp1.Width/2,tmp1.Height/2,myCallback, IntPtr.Zero);//楼主在此调整比例ms = new System.IO.MemoryStream();
img2.Save(ms,System.Drawing.Imaging.ImageFormat.Jpeg);
byte[] newByte = new byte[ms.Length];
ms.Read(newByte,o,ms.Length);
ms.Close();
img1.Dispose();
img2.Dispose();//得到newByte
=============用MemoryStream
{
System.IO.MemoryStream ms = new System.IO.MemoryStream(oldByte);
System.Drawing.Image img1 = System.Drawing.Image.FromStream(ms);
System.Drawing.Image.GetThumbnailImageAbort myCallback = new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback);
System.Drawing.Image img2 = tmp1.GetThumbnailImage(tmp1.Width/2,tmp1.Height/2,myCallback, IntPtr.Zero);//楼主在此调整比例 ms = new System.IO.MemoryStream();
img2.Save(ms,System.Drawing.Imaging.ImageFormat.Jpeg);
byte[] newByte = new byte[ms.Length];
ms.Read(newByte,o,ms.Length);
ms.Close();
img1.Dispose();
img2.Dispose();
return newByte;//得到newByte
}
public static bool ThumbnailCallback()
{
return false;
}
就是用MemoryStream
但遇到与楼主同样的问题
后来放弃了
也许是水平不够
关注一下~
{
System.IO.MemoryStream ms = new System.IO.MemoryStream(oldByte);
System.Drawing.Image img1 = System.Drawing.Image.FromStream(ms);
System.Drawing.Image.GetThumbnailImageAbort myCallback = new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback);
System.Drawing.Image img2 = img1.GetThumbnailImage(img1.Width/2,img1.Height/2,myCallback, IntPtr.Zero);//楼主在此调整比例 ms = new System.IO.MemoryStream();
img2.Save(ms,System.Drawing.Imaging.ImageFormat.Jpeg);
byte[] newByte = new byte[ms.Length];
ms.Position=0;//漏了这个
ms.Read(newByte,0,newByte.Length);
ms.Close();
img1.Dispose();
img2.Dispose(); return newByte;//得到newByte
}
public static bool ThumbnailCallback()
{
return false;
}
byte[] newByte = new byte[ms.Length];
//在这里插入下面的语句。
ms.Read(newByte,o,ms.Length);
ms.Close();
img1.Dispose();
img2.Dispose();=================================================
ms.Seek(0, SeekOrigin.Begin);原因可能是
ms = new System.IO.MemoryStream();
img2.Save(ms,System.Drawing.Imaging.ImageFormat.Jpeg);填充ms流的时候会把流的位置的指针移到了结尾。
所以读的时候就返回空内容了。