ms.GetBuffer() 是把内存里边的东西,保存为2进制格式吗?

wrost 2012-10-18 06:58:41
ms.GetBuffer() 是把内存里边的东西,保存为2进制格式吗?



ms = new MemoryStream();
final_image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
Thumbnail thumb = new Thumbnail(thumbnail_id, ms.GetBuffer() );


完整

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Collections.Generic;

public partial class upload : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

System.Drawing.Image thumbnail_image = null;
System.Drawing.Image original_image = null;
System.Drawing.Bitmap final_image = null;
System.Drawing.Graphics graphic = null;
MemoryStream ms = null;

try
{
// Get the data 得到上传的文件
HttpPostedFile jpeg_image_upload = Request.Files["Filedata"];

// Retrieve the uploaded image 把上传文件转化为 Image
original_image = System.Drawing.Image.FromStream(jpeg_image_upload.InputStream);




// Calculate the new width and height 下边一整段,都是计算长宽的,具体算法没有深究
int width = original_image.Width; //原始宽度
int height = original_image.Height; //原始长度
int target_width = 200;
int target_height = 200;
int new_width, new_height;

float target_ratio = (float)target_width / (float)target_height; //目标 宽长比
float image_ratio = (float)width / (float)height; //原始 宽长比

if (target_ratio > image_ratio) //计算出新的长宽。
{
new_height = target_height;
new_width = (int)Math.Floor(image_ratio * (float)target_height);
}
else
{
new_height = (int)Math.Floor((float)target_width / image_ratio);
new_width = target_width;
}

new_width = new_width > target_width ? target_width : new_width;
new_height = new_height > target_height ? target_height : new_height;




// Create the thumbnail 创建缩略图

// Old way
//thumbnail_image = original_image.GetThumbnailImage(new_width, new_height, null, System.IntPtr.Zero);
// We don't have to create a Thumbnail since the DrawImage method will resize, but the GetThumbnailImage looks better
// I've read about a problem with GetThumbnailImage. If a jpeg has an embedded thumbnail it will use and resize it which
// can result in a tiny 40x40 thumbnail being stretch up to our target size


final_image = new System.Drawing.Bitmap(target_width, target_height); //创建指定大小的位图
graphic = System.Drawing.Graphics.FromImage(final_image); //为什么要通过Bitmap转一下? 应该是可以动态画画
//黑色的画笔,在graphic内画一个指定的矩形
graphic.FillRectangle(new System.Drawing.SolidBrush(System.Drawing.Color.Black), new System.Drawing.Rectangle(0, 0, target_width, target_height));
int paste_x = (target_width - new_width) / 2;
int paste_y = (target_height - new_height) / 2;
graphic.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; /* new way */ //一个图像插值算法
//graphic.DrawImage(thumbnail_image, paste_x, paste_y, new_width, new_height);
graphic.DrawImage(original_image, paste_x, paste_y, new_width, new_height); //载入原始图,按指定大小重新绘制

// Store the thumbnail in the session (Note: this is bad, it will take a lot of memory, but this is just a demo)
ms = new MemoryStream();
final_image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);//不明白,这里并没有建立什么联系???





// Store the data in my custom Thumbnail object
string thumbnail_id = DateTime.Now.ToString("yyyyMMddHHmmssfff"); //设置文件名
Thumbnail thumb = new Thumbnail(thumbnail_id, ms.GetBuffer());

// Put it all in the Session (initialize the session if necessary)
List<Thumbnail> thumbnails = Session["file_info"] as List<Thumbnail>;
if (thumbnails == null)
{
thumbnails = new List<Thumbnail>();
Session["file_info"] = thumbnails;
}
thumbnails.Add(thumb);

Response.StatusCode = 200;
Response.Write(thumbnail_id);
}
catch
{
// If any kind of error occurs return a 500 Internal Server error
Response.StatusCode = 500;
Response.Write("An error occured");
Response.End();
}
finally
{
// Clean up
if (final_image != null) final_image.Dispose();
if (graphic != null) graphic.Dispose();
if (original_image != null) original_image.Dispose();
if (thumbnail_image!= null )thumbnail_image.Dispose();
if (ms != null) ms.Close();
Response.End();
}

}
}

...全文
122 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
wrost 2013-10-21
  • 打赏
  • 举报
回复
引用 2 楼 findcaiyzh 的回复:
如果想保存到byte[]中。应该使用ToArray. MemoryStream.ToArray Method: Writes the stream contents to a byte array, regardless of the Position property. 参考: http://msdn.microsoft.com/en-US/library/system.io.memorystream.toarray(v=vs.80).aspx
谢谢
宝_爸 2012-10-18
  • 打赏
  • 举报
回复
如果想保存到byte[]中。应该使用ToArray.

MemoryStream.ToArray Method:
Writes the stream contents to a byte array, regardless of the Position property.

参考:
http://msdn.microsoft.com/en-US/library/system.io.memorystream.toarray(v=vs.80).aspx
宝_爸 2012-10-18
  • 打赏
  • 举报
回复
GetBuffer:

The byte array from which this stream was created, or the underlying array if a byte array was not provided to the MemoryStream constructor during construction of the current instance.

参考:
http://msdn.microsoft.com/en-US/library/system.io.memorystream.getbuffer(v=vs.80).aspx
using System; using System.Collections.Generic; using System.Text; using System.Net; using System.Net.Sockets; using System.Collections; using System.IO; using System.Text.RegularExpressions; using RE = System.Text.RegularExpressions.Regex; using System.Security.Cryptography.X509Certificates; /*************************************************************************************************************************************************** * *文件名:HttpProc.cs * *创建人:HeDaode * *日 期:2007.09.01 * *描 述:实现HTTP协议中的GET、POST请求 * *使 用:HttpProc.WebClient client = new HttpProc.WebClient(); client.Encoding = System.Text.Encoding.Default;//默认编码方式,根据需要设置其他类型 client.OpenRead("http://www.baidu.com");//普通get请求 MessageBox.Show(client.RespHtml);//获取返回的网页源代码 client.DownloadFile("http://www.codepub.com/upload/163album.rar",@"C:\163album.rar");//下载文件 client.OpenRead("http://passport.baidu.com/?login","username=zhangsan&password=123456");//提交表单,此处是登录百度的示例 client.UploadFile("http://hiup.baidu.com/zhangsan/upload", @"file1=D:\1.mp3");//上传文件 client.UploadFile("http://hiup.baidu.com/zhangsan/upload", "folder=myfolder&size=4003550",@"file1=D:\1.mp3");//提交含文本域和文件域的表单 *****************************************************************************************************************************************************/ namespace HttpProc { /// ///上传事件委托 /// /// /// public delegate void WebClientUploadEvent(object sender, HttpProc.UploadEventArgs e); /// ///下载事件委托 /// /// /// public delegate void WebClientDownloadEvent(object sender, HttpProc.DownloadEventArgs e); /// ///上传事件参数 /// public struct UploadEventArgs { /// ///上传数据总大小 ///

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧