日期:2014-05-17  浏览次数:20359 次

ms = new MemoryStream();
final_image.Save 没有和 graphic 关联, 怎么就把图像给存到final_image里边去了?

C# code

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);//不明白,这里并没有建立什么联系???








完整
C# code


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