migrate to new git
This commit is contained in:
222
web/App_Script/DisplayCut.ashx
Normal file
222
web/App_Script/DisplayCut.ashx
Normal file
@@ -0,0 +1,222 @@
|
||||
<%@ WebHandler Language="C#" Class="DisplayCut" %>
|
||||
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using System.Diagnostics;
|
||||
using System.Web;
|
||||
using System.IO;
|
||||
using System.Drawing;
|
||||
using System.Drawing.Imaging;
|
||||
|
||||
|
||||
public class DisplayCut : IHttpHandler
|
||||
{
|
||||
|
||||
public void ProcessRequest(HttpContext context)
|
||||
{
|
||||
if (context.Request.QueryString["W"] != null & context.Request.QueryString["H"] != null)
|
||||
{
|
||||
string CurrentPath = HttpContext.Current.Server.MapPath("~/upload/");
|
||||
|
||||
string FileName = context.Request.QueryString["File"].Trim().Replace("../", "");
|
||||
int GetWidth = Convert.ToInt32(context.Request.QueryString["W"].Trim());
|
||||
int GetHeight = Convert.ToInt32(context.Request.QueryString["H"].Trim());
|
||||
|
||||
string[] n = Path.GetFileName(FileName).Split('.');
|
||||
string picType = n[n.Length - 1].ToLower();
|
||||
if (picType == "jpg")
|
||||
picType = "jpeg";
|
||||
|
||||
if (picType == "png")
|
||||
picType = "jpeg";
|
||||
|
||||
MyWeb.fileSystem fs = new MyWeb.fileSystem();
|
||||
|
||||
string FileNameAndPath = CurrentPath + FileName;
|
||||
|
||||
if (!System.IO.File.Exists(FileNameAndPath) | !fs.isPhoto(FileName))
|
||||
{
|
||||
FileNameAndPath = HttpContext.Current.Server.MapPath("~/App_Script/noimage_us.jpg");
|
||||
//顯示無圖片
|
||||
picType = "jpeg";
|
||||
}
|
||||
|
||||
context.Response.ContentType = "Image/" + picType.ToUpper();
|
||||
context.Response.Clear();
|
||||
context.Response.Cache.SetExpires(DateTime.Now.AddMinutes(43200));
|
||||
|
||||
//開始處理
|
||||
System.IO.FileInfo Original_FileInfo = new System.IO.FileInfo(FileNameAndPath);
|
||||
|
||||
System.Drawing.Image Original_Image = System.Drawing.Image.FromFile(FileNameAndPath);
|
||||
//讀取圖片
|
||||
|
||||
|
||||
int New_Width = Convert.ToInt32(context.Request.QueryString["W"].Trim());
|
||||
int New_Height = Convert.ToInt32(context.Request.QueryString["H"].Trim());
|
||||
|
||||
int x = 0;
|
||||
int y = 0;
|
||||
|
||||
double temp_w = 1.0;
|
||||
double temp_h = 1.0;
|
||||
|
||||
|
||||
int old_h = Original_Image.Height;
|
||||
int old_w = Original_Image.Width;
|
||||
int new_h = New_Height;
|
||||
int new_w = New_Width;
|
||||
|
||||
|
||||
if (new_h < old_h)
|
||||
{
|
||||
temp_w = Convert.ToDouble((Convert.ToDouble(old_h) - Convert.ToDouble(new_h)) / Convert.ToDouble(old_h));
|
||||
//寬要縮的比例
|
||||
}
|
||||
|
||||
if (new_w < old_w)
|
||||
{
|
||||
temp_h = Convert.ToDouble(Convert.ToDouble(Convert.ToDouble(old_w) - Convert.ToDouble(new_w)) / Convert.ToDouble(old_w));
|
||||
//高要縮的比
|
||||
}
|
||||
|
||||
if (temp_w != temp_h)
|
||||
{
|
||||
if (temp_w > temp_h)
|
||||
{
|
||||
if (temp_w < 1.0)
|
||||
{
|
||||
New_Width = old_w - ((old_h - new_h) * old_w) / old_h;
|
||||
New_Height = old_h - ((old_h - new_h) * old_h) / old_h;
|
||||
}
|
||||
else
|
||||
{
|
||||
New_Width = old_w - ((old_w - new_w) * old_w) / old_w;
|
||||
New_Height = old_h - ((old_w - new_w) * old_h) / old_w;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (temp_w < temp_h)
|
||||
{
|
||||
if (temp_h < 1.0)
|
||||
{
|
||||
New_Width = old_w - ((old_w - new_w) * old_w) / old_w;
|
||||
New_Height = old_h - ((old_w - new_w) * old_h) / old_w;
|
||||
}
|
||||
else
|
||||
{
|
||||
New_Width = old_w - ((old_h - new_h) * old_w) / old_h;
|
||||
New_Height = old_h - ((old_h - new_h) * old_h) / old_h;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
New_Width = old_w;
|
||||
New_Height = old_h;
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (temp_h < 1.0 && temp_w < 1.0)
|
||||
{
|
||||
New_Width = old_w - ((old_w - new_w) * old_w) / old_w;
|
||||
New_Height = old_h - ((old_w - new_w) * old_h) / old_w;
|
||||
}
|
||||
else
|
||||
{
|
||||
New_Width = old_w;
|
||||
New_Height = old_h;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
System.Drawing.Image New_Image = new System.Drawing.Bitmap(GetWidth, GetHeight);
|
||||
System.Drawing.Graphics ObjGraphics = System.Drawing.Graphics.FromImage(New_Image);
|
||||
|
||||
MyWeb.global global = new MyWeb.global();
|
||||
|
||||
//相關設定
|
||||
ObjGraphics.InterpolationMode = global.InterpolationMode;
|
||||
ObjGraphics.SmoothingMode = global.SmoothingMode;
|
||||
ObjGraphics.CompositingQuality = global.CompositingQuality;
|
||||
|
||||
if (picType == "png")
|
||||
{
|
||||
ObjGraphics.Clear(System.Drawing.Color.Transparent);
|
||||
//清空Graphics, 以透明背景色填充
|
||||
}
|
||||
else
|
||||
{
|
||||
ObjGraphics.Clear(System.Drawing.Color.White);
|
||||
//清空Graphics, 以白色填充
|
||||
}
|
||||
|
||||
//在指定位置按指定大小繪制原圖片的片段
|
||||
ObjGraphics.DrawImage(Original_Image, new System.Drawing.Rectangle((GetWidth - New_Width) / 2, (GetHeight - New_Height) / 2, New_Width, New_Height), new System.Drawing.Rectangle(x, y, Original_Image.Width, Original_Image.Height), System.Drawing.GraphicsUnit.Pixel);
|
||||
|
||||
if (fs.WatermarkMode == "1")
|
||||
{
|
||||
FileNameAndPath = HttpContext.Current.Server.MapPath(fs.WatermarkPath);
|
||||
System.Drawing.Image Watermark_Image = System.Drawing.Image.FromFile(FileNameAndPath);
|
||||
int w_Width = Convert.ToInt32(Math.Round(Convert.ToDouble(GetWidth) * 0.8));
|
||||
int w_Height = Convert.ToInt32(Watermark_Image.Height * (Convert.ToDouble(w_Width) / Convert.ToDouble(Watermark_Image.Width)));
|
||||
int w_myX = (GetWidth - w_Width) / 2; //浮水印水平置中
|
||||
int w_myY = (GetHeight - w_Height) / 2 + w_Height; //浮水印水平垂直置中向下偏移一個高度
|
||||
var colorMatrix = new ColorMatrix();
|
||||
colorMatrix.Matrix33 = (float)Convert.ToSingle(fs.WatermarkPct);
|
||||
var imageAttributes = new ImageAttributes();
|
||||
imageAttributes.SetColorMatrix(colorMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
|
||||
ObjGraphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
|
||||
ObjGraphics.DrawImage(
|
||||
Watermark_Image,
|
||||
new Rectangle(w_myX, w_myY, w_Width, w_Height),
|
||||
0,
|
||||
0,
|
||||
Watermark_Image.Width,
|
||||
Watermark_Image.Height,
|
||||
GraphicsUnit.Pixel,
|
||||
imageAttributes);
|
||||
}
|
||||
|
||||
|
||||
//下方設定使JPG質量
|
||||
System.Drawing.Imaging.EncoderParameters EPS = new System.Drawing.Imaging.EncoderParameters();
|
||||
System.Drawing.Imaging.EncoderParameter EP = new System.Drawing.Imaging.EncoderParameter(System.Drawing.Imaging.Encoder.Quality, Convert.ToInt64(global.ImageCompressionQuality));
|
||||
|
||||
|
||||
EPS.Param[0] = EP;
|
||||
|
||||
System.Drawing.Imaging.ImageCodecInfo[] codecs = System.Drawing.Imaging.ImageCodecInfo.GetImageEncoders();
|
||||
|
||||
System.Drawing.Imaging.ImageCodecInfo ICI = null;
|
||||
|
||||
|
||||
foreach (System.Drawing.Imaging.ImageCodecInfo codec in codecs)
|
||||
{
|
||||
if (codec.MimeType.ToLower() == ("image/" + picType).ToLower())
|
||||
{
|
||||
ICI = codec;
|
||||
}
|
||||
}
|
||||
|
||||
New_Image.Save(context.Response.OutputStream, ICI, EPS);
|
||||
|
||||
|
||||
//Clear
|
||||
New_Image.Dispose();
|
||||
ObjGraphics.Dispose();
|
||||
Original_Image.Dispose();
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
public bool IsReusable
|
||||
{
|
||||
get { return false; }
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user