62,050
社区成员
发帖
与我相关
我的任务
分享
/// <summary>
/// 生成缩略图
/// </summary>
/// <param name="originalImagePath">源图路径(物理路径)</param>
/// <param name="thumbnailPath">缩略图路径(物理路径)</param>
/// <param name="width">缩略图宽度</param>
/// <param name="height">缩略图宽度</param>
/// <param name="mode">缩略图高度</param>
/// <param name="type">生成缩略图的方式 </param>
public static void MakeThumbnail(string originalImagePath, string thumbnailPath, int width, int height, string mode, string type)
{
System.Drawing.Image originalImage = System.Drawing.Image.FromFile(originalImagePath);
int towidth = width;
int toheight = height;
int x = 0;
int y = 0;
int ow = originalImage.Width;
int oh = originalImage.Height;
switch (mode)
{
case "HW"://指定高宽缩放(可能变形)
break;
case "W"://指定宽,高按比例
toheight = originalImage.Height * width / originalImage.Width;
break;
case "H"://指定高,宽按比例
towidth = originalImage.Width * height / originalImage.Height;
break;
case "Cut"://指定高宽裁减(不变形)
if ((double)originalImage.Width / (double)originalImage.Height > (double)towidth / (double)toheight)
{
oh = originalImage.Height;
ow = originalImage.Height * towidth / toheight;
y = 0;
x = (originalImage.Width - ow) / 2;
}
else
{
ow = originalImage.Width;
oh = originalImage.Width * height / towidth;
x = 0;
y = (originalImage.Height - oh) / 2;
}
break;
case "DB"://等比缩放(不变形,如果高大按高,宽大按宽缩放)
if ((double)originalImage.Width / (double)towidth < (double)originalImage.Height / (double)toheight)
{
toheight = height;
towidth = originalImage.Width * height / originalImage.Height;
}
else
{
towidth = width;
toheight = originalImage.Height * width / originalImage.Width;
}
break;
default:
break;
}
//新建一个bmp图片
System.Drawing.Image bitmap = new System.Drawing.Bitmap(towidth, toheight);
//新建一个画板
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap);
//设置高质量插值法
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
//设置高质量,低速度呈现平滑程度
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
//清空画布并以透明背景色填充
g.Clear(System.Drawing.Color.Transparent);
//在指定位置并且按指定大小绘制原图片的指定部分
g.DrawImage(originalImage, new System.Drawing.Rectangle(0, 0, towidth, toheight),
new System.Drawing.Rectangle(x, y, ow, oh),
System.Drawing.GraphicsUnit.Pixel);
try
{
//保存缩略图
if (type == "JPG")
{
bitmap.Save(thumbnailPath, System.Drawing.Imaging.ImageFormat.Jpeg);
}
if (type == "BMP")
{
bitmap.Save(thumbnailPath, System.Drawing.Imaging.ImageFormat.Bmp);
}
if (type == "GIF")
{
bitmap.Save(thumbnailPath, System.Drawing.Imaging.ImageFormat.Gif);
}
if (type == "PNG")
{
bitmap.Save(thumbnailPath, System.Drawing.Imaging.ImageFormat.Png);
}
}
catch (System.Exception e)
{
throw e;
}
finally
{
originalImage.Dispose();
bitmap.Dispose();
g.Dispose();
}
}
/**/
///
/// 在图片上增加文字水印
///
/// 原服务器图片路径
/// 生成的带文字水印的图片路径
protected void AddShuiYinWord(string Path, string Path_sy)
{
string addText = "测试水印";
System.Drawing.Image image = System.Drawing.Image.FromFile(Path);
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(image);
g.DrawImage(image, 0, 0, image.Width, image.Height);
System.Drawing.Font f = new System.Drawing.Font("Verdana", 16);
System.Drawing.Brush b = new System.Drawing.SolidBrush(System.Drawing.Color.Blue);
g.DrawString(addText, f, b, 15, 15);
g.Dispose();
image.Save(Path_sy);
image.Dispose();
}
/**/
///
/// 在图片上生成图片水印
///
/// 原服务器图片路径
/// 生成的带图片水印的图片路径
/// 水印图片路径
protected void AddShuiYinPic(string Path, string Path_syp, string Path_sypf)
{
System.Drawing.Image image = System.Drawing.Image.FromFile(Path);
System.Drawing.Image copyImage = System.Drawing.Image.FromFile(Path_sypf);
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(image);
g.DrawImage(copyImage, new System.Drawing.Rectangle(image.Width - copyImage.Width, image.Height - copyImage.Height, copyImage.Width, copyImage.Height), 0, 0, copyImage.Width, copyImage.Height, System.Drawing.GraphicsUnit.Pixel);
g.Dispose();
image.Save(Path_syp);
image.Dispose();
}
/// <summary>
/// 删除文件文件或图片
/// </summary>
/// <param name="path">当前文件的路径</param>
/// <returns>是否删除成功</returns>
public static bool FilePicDelete(string path)
{
bool ret = false;
System.IO.FileInfo file = new System.IO.FileInfo(path);
if (file.Exists)
{
file.Delete();
ret = true;
}
return ret;
}
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE>上传图片前预览 </TITLE>
<script>
//此方法只能用在HTML页面中
function test(){
document.getElementById("showimg").src=document.getElementById("file1").value;
}
/*
google一下能用Microsoft.AlphaImageLoader滤镜解决此问题.此滤镜的主要作用就是对
图片进行透明处理。虽然FireFox和IE7以上的IE浏览器已经支持透明的PNG图片,但是就
IE5-IE6而言还是有一定的意义。
语法:
filter : progid:DXImageTransform.Microsoft.AlphaImageLoader (enabled=bEnabled
, sizingMethod=sSize , src=sURL )
属性:
1 enabled:true/false 是否激活滤镜
2 sizingMethod:
01 crop:剪切图片以适应对象尺寸。
02 image:默认值。增大或减小对象的尺寸边界以适应图片的尺寸。
03 scale:缩放图片以适应对象的尺寸边界
3 src属性是指向图片绝对或相对路径的(必选的);
此方法问题:浏览图片是没问题,可是浏览小图Div会小,浏览大图可能会占据整个页面,
而我们希望以固定的大小显示所有的图片
*/
function ShowImage1(path){
document.getElementById("divShow").filters.item
("DXImageTransform.Microsoft.AlphaImageLoader").src = path;
}
/*
为了解决上面的问题,我们可以使用他的scale属性.可是如果用这个属性,在浏览大图时
,他会缩小原图;浏览小图时,他会放大小图,使小图模糊不清。所以不能用此属性。最
后我们添加下面的setImg(o)方法改变这种情况。
*/
function ShowImage(path){
//处理前是原图,先将其隐藏,
document.all.divShow.style.visibility="hidden";
document.all.divShow.filters.item
("DXImageTransform.Microsoft.AlphaImageLoader").src=path;
document.all.divShow.filters.item
("DXImageTransform.Microsoft.AlphaImageLoader").sizingMethod="image";
//过100毫秒后获取div的宽高.
setTimeout("setImg(document.all.divShow)",100);
//setTimeout() 方法用于在指定的毫秒数后调用函数或计算表达式。
document.all.divShow.style.border="1px solid #ccc";
}
function setImg(o){
var img_width;
var img_height;
o.style.visibility = "visible";
img_width=o.offsetWidth; //o.offsetWidth对象的可见宽度
img_height=o.offsetHeight; //o.offsetWidth对象的可见高度
var width=274; //预定义宽
var height=100; //预定义高
var ratW; //宽的缩小比例
var ratH; //高的缩小比例
var rat; //实际使用的缩小比例
if(img_width<width && img_height<height)
{
//如果比预定义的宽高都小,原图显示。
o.filters.item
("DXImageTransform.Microsoft.AlphaImageLoader").sizingMethod="image";
return;
}else{
//如果大的话,要把 sizingMethod改成scale; 如果属性是image,不管怎么改div的
宽高,都不起作用
o.filters.item
("DXImageTransform.Microsoft.AlphaImageLoader").sizingMethod="scale";
}
ratW=width/img_width;
ratH=height/img_height;
if(ratH<ratW)//选择最小的作为实际的缩小比例
rat=ratH;
else
rat=ratW;
img_width=img_width * rat;
img_height=img_height * rat;
o.style.width=img_width;
o.style.height=img_height;
}
</script>
</HEAD>
<BODY MS_POSITIONING="GridLayout">
<!--
<img id="showimg">
<input type="file" id="file1" onchange="test()">
-->
<form id="Form1" method="post">
<input type="file" onchange="ShowImage(this.value)" >
<div id="divShow"
style="width:274px;height:100px;FILTER:progid:DXImageTransform.Microsoft.Alpha
ImageLoader(sizingMethod=image);"></div>
</form>
</BODY>
</HTML>
/// <summary>
///
/// </summary>
/// <param name="fileurl">原图URL</param>
/// <param name="minwidth">缩略图宽</param>
/// <param name="minheight">缩略图高</param>
/// <param name="FileName">上传到“”文件夹下目录(文件夹名)</param>
/// <param name="filename">原图文件名</param>
/// <returns></returns>
public static void SendToSmallImage(string fileurl, int minwidth, int minheight, string FileName,string filename)
{
//public static string SendToSmallImage(Stream _stream, int minwidth, int minheight, string FileName, bool type)
//System.Drawing.Image img = System.Drawing.Image.FromStream(_stream);
System.Drawing.Image img = System.Drawing.Image.FromFile(HttpContext.Current.Server.MapPath(fileurl));
int lenEnd = fileurl.LastIndexOf('.');
string fileType = fileurl.Substring(lenEnd, fileurl.Length - lenEnd);
ImageFormat imgformat = img.RawFormat;
Size newSize = NewSize(minwidth, minheight, img.Width, img.Height);
Bitmap bm = new Bitmap(newSize.Width, newSize.Height);
Graphics g = Graphics.FromImage(bm);
// 设置画布的描绘质量
g.CompositingQuality = CompositingQuality.HighQuality;
g.SmoothingMode = SmoothingMode.HighQuality;
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.DrawImage(img, new Rectangle(0, 0, newSize.Width, newSize.Height), 0, 0, img.Width, img.Height, GraphicsUnit.Pixel);
g.Dispose();
// 以下代码为保存图片时,设置压缩质量
EncoderParameters encoderParams = new EncoderParameters();
long[] quality = new long[1];
quality[0] = 100;
EncoderParameter encoderParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, quality);
encoderParams.Param[0] = encoderParam;
//获得包含有关内置图像编码解码器的信息的ImageCodecInfo 对象。
ImageCodecInfo[] arrayICI = ImageCodecInfo.GetImageEncoders();
ImageCodecInfo jpegICI = null;
ImageCodecInfo GIFICI = null;
ImageCodecInfo jpgICI = null;
ImageCodecInfo pngICI = null;
ImageCodecInfo bmpICI = null;
ImageCodecInfo allICI = null;
for (int x = 0; x < arrayICI.Length; x++)
{
if (arrayICI[x].FormatDescription.Equals("JPEG"))
jpegICI = arrayICI[x];//设置JPEG编码
if (arrayICI[x].FormatDescription.Equals("GIF"))
GIFICI = arrayICI[x];//设置GIF编码
if (arrayICI[x].FormatDescription.Equals("JPG"))
jpgICI = arrayICI[x];//设置GIF编码
if (arrayICI[x].FormatDescription.Equals("PNG"))
pngICI = arrayICI[x];//设置GIF编码
if (arrayICI[x].FormatDescription.Equals("BMP"))
bmpICI = arrayICI[x];//设置GIF编码
}
string stringpath = null;
string imagename = filename;
if (fileType == ".jpg")
{
stringpath = "/imageUpload/" + FileName + "/" + imagename;
allICI = jpegICI;
}
else if (fileType == ".gif")
{
stringpath = "/imageUpload/" + FileName + "/" + imagename;
allICI = GIFICI;
}
else if (fileType == ".jpg")
{
stringpath = "/imageUpload/" + FileName + "/" + imagename;
allICI = jpgICI;
}
else if (fileType == ".png")
{
stringpath = "/imageUpload/" + FileName + "/" + imagename;
allICI = pngICI;
}
else
{
stringpath = "/imageUpload/" + FileName + "/" + imagename;
allICI = bmpICI;
}
try
{
bm.Save(HttpContext.Current.Server.MapPath(stringpath), allICI, encoderParams);
}
catch (Exception e)
{
throw e;
}
finally
{
img.Dispose();
bm.Dispose();
}
//return imagename;
}
/// <summary>
///
/// </summary>
/// <param name="minwidth">微缩图要设置的宽度</param>
/// <param name="minheight">微缩图要设置的长度</param>
/// <param name="forwidth">原图宽度</param>
/// <param name="forheight">原图长度</param>
/// <returns></returns>
private static Size NewSize(int minwidth, int minheight, int forwidth, int forheight)
{
int newwidth;
int newheight;
if (forwidth > forheight)
{
newwidth = minwidth;
newheight = newwidth * forheight / forwidth;
}
else
{
newheight = minheight;
newwidth = newheight * forwidth / forheight;
}
System.Drawing.Size _Size = new Size();
_Size.Height = newheight;
_Size.Width = newwidth;
return _Size;
}
//用于控制图片缩略图显示
function zoom(image, width, height)
{
var scale = image.width/image.height;
if (image.width > width || image.height > height)
{
// 按比例转换
image.width = scale * height < width ? (height * scale) : width;
image.height = scale * height >= width ? (width / scale) : height;
}
}
/// <summary>
/// 产生原图的缩略图
/// </summary>
/// <param name="souceImage">原图 Image 对象</param>
/// <param name="ImageUrl">原图路径</param>
/// <param name="wight">宽度</param>
/// <param name="height">高度</param>
/// <returns></returns>
public static void ZoomImage(Image souceImage, string ImageUrl, int wight, int height,int languageType)
{
//获得图片的绝对路径
string NewImageUrl = System.Web.HttpContext.Current.Server.MapPath(@ImageUrl);
//赋Image对象 路径
souceImage.ImageUrl = ImageUrl;
if (File.Exists(NewImageUrl))//该图片是存在
{
//将指定图片转成数据流,建Drawing.Image对象
FileStream Pic = new FileStream(NewImageUrl, FileMode.Open, FileAccess.Read, FileShare.Read);
byte[] PicByte = new byte[Pic.Length];
Pic.Read(PicByte, 0, PicByte.Length);
System.Drawing.Image img = System.Drawing.Image.FromStream(Pic);
double scale = (float)img.Width / img.Height;
if (img.Width > wight || img.Height > height)
{
souceImage.Width = Unit.Pixel(Convert.ToInt32(Math.Round((scale * height < wight ? (scale * height) : wight), 0)));
souceImage.Height = Unit.Pixel(Convert.ToInt32(Math.Round((scale * height >= wight ? (wight/scale) : height), 0)));
}
Pic.Dispose();
img.Dispose();
}
else//该图片不存在
{
if (1 == languageType)
{
souceImage.ImageUrl = "~/images/noneImageEn.jpg";//给Image路径赋个无图片的图片
}
else
{
souceImage.ImageUrl = "~/images/noneImage.jpg";//给Image路径赋个无图片的图片
}
souceImage.Width = Unit.Pixel(wight);
souceImage.Height = Unit.Pixel(height);
}
}