您的位置:首页 > 编程语言 > ASP

asp.net 上传图片生成缩略图

2011-07-27 21:59 253 查看
/**/
/// <summary>
/// 在图片上增加文字水印
/// </summary>
/// <param name="Path">原服务器图片路径</param>
/// <param name="Path_sy">生成的带文字水印的图片路径</param>
protected void AddWater(string Path, string Path_sy)
{
string addText = "51aspx.com";
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", 60);
System.Drawing.Brush b = new System.Drawing.SolidBrush(System.Drawing.Color.Green);
g.DrawString(addText, f, b, 35, 35);
g.Dispose();
image.Save(Path_sy);
image.Dispose();
}


/// <summary>
/// 在图片上生成图片水印
/// </summary>
/// <param name="Path">原服务器图片路径</param>
/// <param name="Path_syp">生成的带图片水印的图片路径</param>
/// <param name="Path_sypf">水印图片路径</param>
protected void AddWaterPic(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>
/// <mce:script type="text/javascript" src="http://hi.images.csdn.net/js/blog/tiny_mce/themes/advanced/langs/zh.js" mce_src="http://hi.images.csdn.net/js/blog/tiny_mce/themes/advanced/langs/zh.js"></mce:script><mce:script type="text/javascript" src="http://hi.images.csdn.net/js/blog/tiny_mce/plugins/syntaxhl/langs/zh.js" mce_src="http://hi.images.csdn.net/js/blog/tiny_mce/plugins/syntaxhl/langs/zh.js"></mce:script>amp;lt;param name="filePath">原图片地址</param>
/// <param name="waterFile">水印图片地址</param>
public void MarkWater(string filePaths, string waterFile)
{
//GIF不水印
int i = filePaths.LastIndexOf(".");
string ex = filePaths.Substring(i, filePaths.Length - i);
if (string.Compare(ex, ".gif", true) == 0)
{
return;
}
string ModifyImagePath = filePaths;// FilePath + filePaths;//修改的图像路径
int lucencyPercent = 25;
System.Drawing.Image modifyImage = null;
System.Drawing.Image drawedImage = null;
Graphics g = null;
try
{
//建立图形对象
modifyImage = System.Drawing.Image.FromFile(ModifyImagePath, true);
//  drawedImage = System.Drawing.Image.FromFile(FilePath + waterFile, true);
drawedImage = System.Drawing.Image.FromFile(waterFile, true);
g = Graphics.FromImage(modifyImage);
//获取要绘制图形坐标
int x = modifyImage.Width - drawedImage.Width;
int y = modifyImage.Height - drawedImage.Height;
//设置颜色矩阵
float[][] matrixItems ={
new float[] {1, 0, 0, 0, 0},
new float[] {0, 1, 0, 0, 0},
new float[] {0, 0, 1, 0, 0},
new float[] {0, 0, 0, (float)lucencyPercent/1f, 0},
new float[] {0, 0, 0, 0, 1}};
ColorMatrix colorMatrix = new ColorMatrix(matrixItems);
ImageAttributes imgAttr = new ImageAttributes();
imgAttr.SetColorMatrix(colorMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
//绘制阴影图像
g.DrawImage(drawedImage, new Rectangle(x, y, drawedImage.Width, drawedImage.Height), 0, 0, drawedImage.Width, drawedImage.Height, GraphicsUnit.Pixel, imgAttr);
//保存文件
string[] allowImageType ={ ".jpg", ".gif", ".png", ".bmp", ".tiff", ".wmf", ".ico" };
FileInfo fi = new FileInfo(ModifyImagePath);
ImageFormat imageType = ImageFormat.Gif;
switch (fi.Extension.ToLower())
{
case ".jpg":
imageType = ImageFormat.Jpeg;
break;
case ".gif":
imageType = ImageFormat.Gif;
break;
case ".png":
imageType = ImageFormat.Png;
break;
case ".bmp":
imageType = ImageFormat.Bmp;
break;
case ".tif":
imageType = ImageFormat.Tiff;
break;
case ".wmf":
imageType = ImageFormat.Wmf;
break;
case ".ico":
imageType = ImageFormat.Icon;
break;
default:
break;
}
MemoryStream ms = new MemoryStream();
modifyImage.Save(ms, imageType);
byte[] imgData = ms.ToArray();
modifyImage.Dispose();
drawedImage.Dispose();
g.Dispose();
FileStream fs = null;
File.Delete(ModifyImagePath);
fs = new FileStream(ModifyImagePath, FileMode.Create, FileAccess.Write);
if (fs != null)
{
fs.Write(imgData, 0, imgData.Length);
fs.Close();
}
}
finally
{
try
{
drawedImage.Dispose();
modifyImage.Dispose();
g.Dispose();
}
catch { ;}
}
}


protected void Button1_Click(object sender, EventArgs e)
{
string tt=string.Empty;
if (FileUpload1.HasFile)
{
string fileContentType = FileUpload1.PostedFile.ContentType;
if (fileContentType == "image/bmp" || fileContentType == "image/gif" || fileContentType == "image/pjpeg")
{
string name = FileUpload1.PostedFile.FileName;                  // 客户端文件路径
FileInfo file = new FileInfo(name);
string fileName = file.Name;                                    // 文件名称
tt=fileName;
string fileName_s = "s_" + file.Name;                           // 缩略图文件名称
string fileName_sy = "sy_" + file.Name;                         // 水印图文件名称(文字)
string fileName_syp = "syp_" + file.Name;                       // 水印图文件名称(图片)
string webFilePath = Server.MapPath("file/" + fileName);        // 服务器端文件路径
string webFilePath_s = Server.MapPath("file/" + fileName_s);  // 服务器端缩略图路径
string webFilePath_sy = Server.MapPath("file/" + fileName_sy); // 服务器端带水印图路径(文字)
string webFilePath_syp = Server.MapPath("file/" + fileName_syp); // 服务器端带水印图路径(图片)
string webFilePath_sypf = Server.MapPath("file/shuiyin.jpg"); // 服务器端水印图路径(图片)

if (!File.Exists(webFilePath))
{
try
{

FileUpload1.SaveAs(webFilePath);                                // 使用 SaveAs 方法保存文件
AddWater(webFilePath, webFilePath_sy);
AddWaterPic(webFilePath, webFilePath_syp, webFilePath_sypf);
MakeThumbnail(webFilePath, webFilePath_s, 130, 130, "Cut");     // 生成缩略图方法
MarkWater(webFilePath, webFilePath_sy);
Label1.Text = "提示:文件“" + fileName + "”成功上传,并生成“" + fileName_s + "”缩略图,文件类型为:" + FileUpload1.PostedFile.ContentType + ",文件大小为:" + FileUpload1.PostedFile.ContentLength + "B";
this.Image1.ImageUrl = "file/" + fileName_s;
}
catch (Exception ex)
{
Label1.Text = "提示:文件上传失败,失败原因:" + ex.Message;
}
}
else
{
Label1.Text = "提示:文件已经存在,请重命名后上传";
}
}
else
{
Label1.Text = "提示:文件类型不符";
}
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: