C#圖片上傳伺服器縮放儲存功能

來源:互聯網
上載者:User

標籤:blog   http   color   io   os   使用   ar   java   for   

項目需求上需要使用者上傳頭像。本來是用第三方的外掛程式的。但是很多都是收費的。

不過,我需要花這錢嗎?是不?所以網路上搜集了些資料。。

果然。這個世界 大神是很多很多的。

用了個免費外掛程式。

 <script src="../Scripts/ajaxFileUpload.js" type="text/javascript"></script>

 這玩意兒真心強大。

圖片傳到伺服器了。

然後問題來了。圖片要縮放。本來是想裁剪的。

不過。真心很煩。。裁剪也做出來了。不過我沒有使用。原因是因為。介面就比較亂了。

最後想出了。上傳圖片。利用這個外掛程式傳送到伺服器。

然後在伺服器端使用代碼將照片裁剪成頭像。也就是縮放。

具體代碼:

  public void ProcessRequest(HttpContext context)        {            context.Response.ContentType = "text/plain";            string action = context.Request["myaction"].ToString();            if (action == "updata")            {                string msg = "";                string result = "0";                HttpPostedFile file = context.Request.Files[0];                string ext = Path.GetExtension(file.FileName).ToLower();                string newName = System.DateTime.Now.ToString("yyyyMMddHHmmss");                string tempPath = "upload/temp/";                string img = tempPath + newName + ext;                string filePath = context.Server.MapPath(img);                tempPath = context.Server.MapPath(tempPath);                if (!Directory.Exists(tempPath))                {                    Directory.CreateDirectory(tempPath);                }                using (System.Drawing.Image originalImage = System.Drawing.Image.FromStream(file.InputStream))                {                    int w = originalImage.Width;                    int h = originalImage.Height;                    if (w > 1400 || h > 1400)                    {                        msg = "請您上傳大小在1400*1400以內的圖片";                    }                    else if (w < 50 || h < 50)                    {                        msg = "請您上傳大於50*50的圖片";                    }                    else                    {                        if (w > 300 || h > 300)                        {                            float sizeM;                            using (System.Drawing.Image thumb = GetThumbImage(originalImage, 200, 200, out sizeM))                            {                                thumb.Save(filePath, System.Drawing.Imaging.ImageFormat.Jpeg);                                result = "1";                            }                        }                        else                        {                        }                        //file.SaveAs(filePath);//儲存                    }                }                //result=0;失敗;1;成功; msg:提示資訊                string strWrite = "../ashx/"+img;                context.Response.Write(strWrite);            }        }

 根據網上的一些代碼修改的。這算變通實現了嗎?反正效果是有了。

新問題也來了。。。照片APP那邊要弄成圓的。

而APP端 又不會處理IMG控制項 成圓的 。所以就要求我把圖片在伺服器端直接畫成圓的。

百度了半天。。找到了個 比較好的代碼。

也就是把img圖片圓角化。用了CSDN上的一個類。連結我忘記了。不過代碼可以貼出來。

using System;using System.Data;using System.Configuration;using System.Web;using System.Drawing;using System.Drawing.Drawing2D;using System.Drawing.Imaging;/// <summary>/// ImageOption 的摘要說明/// </summary>public class ImageOperation{      /// <summary>    /// 圓角產生(但是只能是一個角)    /// </summary>    /// <param name="image">源圖片 Image</param>    /// <param name="roundCorner">圓角位置</param>    /// <returns>處理好的Image</returns>    public static Image CreateRoundedCorner(Image image, RoundRectanglePosition roundCorner)    {        Graphics g = Graphics.FromImage(image);        //保證圖片品質        g.SmoothingMode = SmoothingMode.HighQuality;        g.InterpolationMode = InterpolationMode.HighQualityBicubic;        g.CompositingQuality = CompositingQuality.HighQuality;        Rectangle rect = new Rectangle(0, 0, image.Width, image.Height);        //構建圓角外部路徑        GraphicsPath rectPath = CreateRoundRectanglePath(rect, image.Width / 10, roundCorner);         //圓角背景用白色填充        Brush b = new SolidBrush(Color.White);        g.DrawPath(new Pen(b), rectPath);        g.FillPath(b, rectPath);        g.Dispose();        return image;    }    /// <summary>    /// 靶心圖表片的圓角位置    /// </summary>    public enum RoundRectanglePosition    {        /// <summary>        /// 左上方        /// </summary>        TopLeft,        /// <summary>        /// 右上方        /// </summary>        TopRight,        /// <summary>        /// 左下角        /// </summary>        BottomLeft,        /// <summary>        /// 右下角        /// </summary>        BottomRight    }    /// <summary>    /// 構建GraphicsPath路徑    /// </summary>    /// <param name="rect"></param>    /// <param name="radius"></param>    /// <param name="rrPosition">圖片圓角位置</param>    /// <returns>返回GraphicPath</returns>    private static GraphicsPath CreateRoundRectanglePath(Rectangle rect, int radius, RoundRectanglePosition rrPosition)    {        GraphicsPath rectPath = new GraphicsPath();        switch (rrPosition)        {            case RoundRectanglePosition.TopLeft:                {                    rectPath.AddArc(rect.Left, rect.Top, radius * 2, radius * 2, 180, 90);                    rectPath.AddLine(rect.Left, rect.Top, rect.Left, rect.Top + radius);                    break;                }            case RoundRectanglePosition.TopRight:                {                    rectPath.AddArc(rect.Right - radius * 2, rect.Top, radius * 2, radius * 2, 270, 90);                    rectPath.AddLine(rect.Right, rect.Top, rect.Right - radius, rect.Top);                    break;                }            case RoundRectanglePosition.BottomLeft:                {                    rectPath.AddArc(rect.Left, rect.Bottom - radius * 2, radius * 2, radius * 2, 90, 90);                    rectPath.AddLine(rect.Left, rect.Bottom - radius, rect.Left, rect.Bottom);                    break;                }            case RoundRectanglePosition.BottomRight:                {                    rectPath.AddArc(rect.Right - radius * 2, rect.Bottom - radius * 2, radius * 2, radius * 2, 0, 90);                    rectPath.AddLine(rect.Right - radius, rect.Bottom, rect.Right, rect.Bottom);                    break;                }        }        return rectPath;    }    /// <summary>    /// 圖片縮放    /// </summary>    /// <param name="b">源圖片Bitmap</param>    /// <param name="dstWidth">目標寬度</param>    /// <param name="dstHeight">目標高度</param>    /// <returns>處理完成的圖片 Bitmap</returns>    public static Bitmap ResizeBitmap(Bitmap b, int dstWidth, int dstHeight)    {        Bitmap dstImage = new Bitmap(dstWidth, dstHeight);        System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(dstImage);        //   設定插值模式         g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.Bilinear;        //   設定平滑模式         g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;        g.SmoothingMode = SmoothingMode.HighQuality;        g.InterpolationMode = InterpolationMode.HighQualityBicubic;        //用Graphic的DrawImage方法通過設定大小繪製新的圖片實現縮放        g.DrawImage(b, new Rectangle(0, 0, dstImage.Width, dstImage.Height), new Rectangle(0, 0, b.Width, b.Height),GraphicsUnit.Pixel);        g.Save();        g.Dispose();        return dstImage;    }}

 這樣應該就可以了。。。。。至此。上傳圖片顯示成使用者圓形頭像功能。基本實現。

但是效率不是很高。一般的做法是吧IMG 控制項給修改掉。這樣 一勞永逸。。。

對於我而言。功能做到。怎麼實現。大家有何高見?

C#圖片上傳伺服器縮放儲存功能

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.