184 lines
6.9 KiB
Plaintext
184 lines
6.9 KiB
Plaintext
<%@ WebHandler Language="C#" Class="DisplayCut2" %>
|
|
|
|
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 DisplayCut2 : 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;
|
|
|
|
int old_h = Original_Image.Height;
|
|
int old_w = Original_Image.Width;
|
|
|
|
int value_h = Convert.ToInt32(old_h * (Convert.ToDouble(New_Width) / Convert.ToDouble(old_w)));
|
|
int value_w = Convert.ToInt32(old_w * (Convert.ToDouble(New_Height) / Convert.ToDouble(old_h)));
|
|
|
|
if (New_Width > New_Height)
|
|
{
|
|
|
|
if (old_w < old_h)
|
|
{
|
|
if (New_Height < value_h) { New_Height = value_h; } else { New_Width = value_w; }
|
|
}
|
|
else
|
|
{
|
|
if (New_Width < value_w) { New_Width = value_w; } else { New_Height = value_h; }
|
|
}
|
|
|
|
}
|
|
else
|
|
{
|
|
if (old_w > old_h)
|
|
{
|
|
if (New_Width < value_w) { New_Width = value_w; } else { New_Height = value_h; }
|
|
}
|
|
else
|
|
{
|
|
if (New_Height < value_h) { New_Height = value_h; } else { New_Width = value_w; }
|
|
}
|
|
|
|
}
|
|
|
|
|
|
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; }
|
|
}
|
|
|
|
}
|