簡體   English   中英

C#裁剪然后縮放裁剪圖像

[英]C# Crop then scale the Cropped Image

我正在嘗試構建這個類(在ASP.NET站點中使用),它將裁剪圖像給定自定義寬度,高度X,Y,然后獲取結果圖像並將其縮放到自定義寬度,高度,並保存在目錄上服務器返回此圖像的網址。

我會像這樣在querystring中得到這些參數

Default.aspx?x=100&y=300&w=800&h=500&scalew=160&scaleh=100 

所以這就是我到目前為止所得到的

    public static Image CustomCrop(int width, int height, int x, int y, int scalwidth, int scalheight)
    {
        try
        {
            Image image = Image.FromFile("Images/none.jpg");
            Bitmap bmp = new Bitmap(width, height, PixelFormat.Format24bppRgb);
            bmp.SetResolution(80, 60);

            Graphics gfx = Graphics.FromImage(bmp);
            gfx.SmoothingMode = SmoothingMode.AntiAlias;
            gfx.InterpolationMode = InterpolationMode.HighQualityBicubic;
            gfx.PixelOffsetMode = PixelOffsetMode.HighQuality;
            gfx.DrawImage(image, new Rectangle(0, 0, width, height), x, y, width, height, GraphicsUnit.Pixel);


            return bmp;
        }
        catch (Exception ex)
        {
            //MessageBox.Show(ex.Message);
            return null;
        }
    }

我將發送這些值裁剪圖像(寬度,高度,x,y)然后縮放Croped圖像(scalwidth,scalheight)然后將jpg保存在目錄中並返回圖像位置的url

那么最好的方法是什么?

在asp.net網站或應用程序中創建一個Generic Handler (即ashx文件),並在其中放置以下代碼。 稱它為例如“Handler.ashx”。

現在,在瀏覽器中使用: Handler.ashx?x=100&y=300&w=800&h=500&scalew=160&scaleh=100

Handler.ashx文件代碼:

<%@ WebHandler Language="C#" Class="Handler" %>

using System;
using System.Web;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;

public class Handler : IHttpHandler {

    public void ProcessRequest (HttpContext context) {
        context.Response.ContentType = "image/jpeg";
        int x = int.Parse(context.Request["x"]);
        int y = int.Parse(context.Request["y"]);
        int h = int.Parse(context.Request["h"]);
        int w = int.Parse(context.Request["w"]);
        int scalew = int.Parse(context.Request["scalew"]);
        int scaleh = int.Parse(context.Request["scaleh"]);
        using (Image img = CustomCrop(w, h, x, y, scalew, scaleh))
            img.Save(context.Response.OutputStream,ImageFormat.Jpeg); 
    }

    public static Image CustomCrop(int width, int height, int x, int y, int scalwidth, int scalheight)
    {
        try
        {
            Image image = Image.FromFile("c:\\x.jpg");
            Bitmap bmp = new Bitmap(scalwidth, scalheight, PixelFormat.Format24bppRgb);
            bmp.SetResolution(80, 60);

            Graphics gfx = Graphics.FromImage(bmp);
            gfx.SmoothingMode = SmoothingMode.AntiAlias;
            gfx.InterpolationMode = InterpolationMode.HighQualityBicubic;
            gfx.PixelOffsetMode = PixelOffsetMode.HighQuality;
            gfx.DrawImage(image, new Rectangle(0, 0, scalwidth, scalheight), x, y, width, height, GraphicsUnit.Pixel);


            return bmp;
        }
        catch (Exception ex)
        {
            //MessageBox.Show(ex.Message);
            return null;
        }
    }
    public bool IsReusable {
        get {
            return false;
        }
    }

}

編輯:

關於通用處理程序的一些參考:

HTTP處理程序和HTTP模塊概述

@ WebHandler - ashx文件的工作原理。

好吧,你已經有了工作代碼,這確實是其中一種方式 - 你可以添加壓縮(例如,JPEG格式會使用有損壓縮) - 請參閱這篇文章: http//www.britishdeveloper.co.uk/2011/ 05 /圖像調整大小-裁剪和- compression.html

但是, 我建議不要使用System.Drawing命名空間 根據MSDN文檔 ,不支持在ASP.NET應用程序中使用GDI +(即System.Drawing)。 相反,它推薦使用Windows Imaging Components(即從.NET角度使用WPF Imaging - 它在內部使用WIC)。 看到這篇文章將開始使用WPF來裁剪/縮放圖像: http//weblogs.asp.net/bleroy/archive/2009/12/10/resizing-images-from-the-server-using-wpf-wic -instead-的-gdi.aspx

已經存在經過充分測試和驗證的開源庫 - imageresizing.net ,它支持GDI +,WIC和FreeImage后端。 我在2009年寫了它,並從那時起發布了60個新版本。 它已在超過10,000個網站上使用,並為一些非常大的社交網站提供支持。

System.Drawing 幾乎不可能正確使用。 我已經記錄了大約30個陷阱 ,並且很快就會有更多陷阱 它可以安全地完成,但你不會通過復制和粘貼代碼來實現它。

使用包裝器比從端到端處理內存管理更好,並避免所有GDI +和ASP.NET陷阱。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM