62,267
社区成员
发帖
与我相关
我的任务
分享protected void Button1_Click(object sender, EventArgs e)
{
if (this.flImage.HasFile)
{
Graphics g = null;
System.Drawing.Image upImage = null;
System.Drawing.Image thumimg = null;
System.Drawing.Image simage = null;
Bitmap outputFile = null;
string filename1 = flImage.PostedFile.FileName;
int pos = filename1.LastIndexOf(".");
string extension = filename1.Substring((pos + 1)).ToLower();
if (extension != "jpg" && extension != "gif" && extension != "jpeg" && extension != "bmp")
{
ChangeHope_fc.Show_Msg("提示:图片格式必须是bmp、jpeg、gif、jpg中的一种!", "YX_Upfile4.aspx");
}
string fileName = DateTime.Now.ToString("yyyyMMddhhmmss").ToLower();//用时间来定义文件名,以免重复
try
{
//保存路径
string savedPath = Server.MapPath("~") + "\\YX_UpFile\\Big\\";
string saveSnPath = Server.MapPath("~") + "\\YX_UpFile\\SNPic\\";
int width, height, newWidth, newHeight;
System.Drawing.Image.GetThumbnailImageAbort callb = new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback);
//水印文件
simage = System.Drawing.Image.FromFile(Server.MapPath("~/YX_UpFile/SNPic/yixiang.jpg"));
//保存上传的图片
upImage = System.Drawing.Image.FromStream(flImage.PostedFile.InputStream);
//宽
width = upImage.Width;
//高
height = upImage.Height;
if (width > height)
{
newWidth = 200;
newHeight = (int)((double)height / width * newWidth);
}
else
{
newHeight = 200;
newWidth = (int)((double)width / height * newHeight);
}
//保存缩略图的各项长 宽 高
thumimg = upImage.GetThumbnailImage(newWidth, newHeight, callb, IntPtr.Zero);
outputFile = new Bitmap(upImage);
g = Graphics.FromImage(outputFile);
// 在上传的图片上绘制水印图片。
g.DrawImage(simage,
new Rectangle(upImage.Width - simage.Width, upImage.Height - simage.Height, upImage.Width, upImage.Height),
0,
0,
upImage.Width,
upImage.Height,
GraphicsUnit.Pixel);
string newPath = savedPath + fileName + "." + extension;// 带水印的大图片路径
string thumPath = saveSnPath + fileName + "." + extension;// 缩略图路径
string big = "YX_UpFile/Big/" + fileName + "." + extension;// 保存带水印的大图片
string small = "YX_UpFile/SNPic/" + fileName + "." + extension;
outputFile.Save(newPath); //保存大图片
thumimg.Save(thumPath);// 保存缩略图
Response.Write("<script>parent.document.form1.imgUrl2.value+='" + big + "|';</script>");
Response.Write("<script>parent.document.form1.imgUrl3.value+='" + small + "|';</script>");
outputFile.Dispose();
}
catch
{
ChangeHope_fc.Show_Msg("对不起,为图片添加水印图片时失败,请重试!", "YX_Upfile4.aspx");
}
finally
{
if (g != null)
{
g.Dispose();
}
if (thumimg != null)
{
thumimg.Dispose();
}
if (upImage != null)
{
upImage.Dispose();
}
if (simage != null)
{
simage.Dispose();
}
}
}
else
{
ChangeHope_CMS.Show_Msg("提示:请您选择文件!");
}
}
private bool ThumbnailCallback()
{
return false;
}protected void Button1_Click(object sender, EventArgs e)
{
if (this.flImage.HasFile)
{
Graphics g = null;
System.Drawing.Image upImage = null;
System.Drawing.Image thumimg = null;
System.Drawing.Image simage = null;
Bitmap outputFile = null;
string filename1 = flImage.PostedFile.FileName;
int pos = filename1.LastIndexOf(".");
string extension = filename1.Substring((pos + 1)).ToLower();
if (extension != "jpg" && extension != "gif" && extension != "jpeg" && extension != "bmp")
{
ChangeHope_fc.Show_Msg("提示:图片格式必须是bmp、jpeg、gif、jpg中的一种!", "YX_Upfile4.aspx");
}
string fileName = DateTime.Now.ToString("yyyyMMddhhmmss").ToLower();//用时间来定义文件名,以免重复
try
{
//保存路径
string savedPath = Server.MapPath("~") + "\\YX_UpFile\\Big\\";
string saveSnPath = Server.MapPath("~") + "\\YX_UpFile\\SNPic\\";
int width, height, newWidth, newHeight;
System.Drawing.Image.GetThumbnailImageAbort callb = new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback);
//水印文件
simage = System.Drawing.Image.FromFile(Server.MapPath("~/YX_UpFile/SNPic/yixiang.jpg"));
//保存上传的图片
upImage = System.Drawing.Image.FromStream(flImage.PostedFile.InputStream);
//宽
width = upImage.Width;
//高
height = upImage.Height;
if (width > height)
{
newWidth = 200;
newHeight = (int)((double)height / width * newWidth);
}
else
{
newHeight = 200;
newWidth = (int)((double)width / height * newHeight);
}
//保存缩略图的各项长 宽 高
thumimg = upImage.GetThumbnailImage(newWidth, newHeight, callb, IntPtr.Zero);
outputFile = new Bitmap(upImage);
g = Graphics.FromImage(outputFile);
// 在上传的图片上绘制水印图片。
g.DrawImage(simage,
new Rectangle(upImage.Width - simage.Width, upImage.Height - simage.Height, upImage.Width, upImage.Height),
0,
0,
upImage.Width,
upImage.Height,
GraphicsUnit.Pixel);
string newPath = savedPath + fileName + "." + extension;// 带水印的大图片路径
string thumPath = saveSnPath + fileName + "." + extension;// 缩略图路径
string big = "YX_UpFile/Big/" + fileName + "." + extension;// 保存带水印的大图片
string small = "YX_UpFile/SNPic/" + fileName + "." + extension;
outputFile.Save(newPath); //保存大图片
thumimg.Save(thumPath);// 保存缩略图
Response.Write("<script>parent.document.form1.imgUrl2.value+='" + big + "|';</script>");
Response.Write("<script>parent.document.form1.imgUrl3.value+='" + small + "|';</script>");
outputFile.Dispose();
}
catch
{
ChangeHope_fc.Show_Msg("对不起,为图片添加水印图片时失败,请重试!", "YX_Upfile4.aspx");
}
finally
{
if (g != null)
{
g.Dispose();
}
if (thumimg != null)
{
thumimg.Dispose();
}
if (upImage != null)
{
upImage.Dispose();
}
if (simage != null)
{
simage.Dispose();
}
}
}
else
{
ChangeHope_CMS.Show_Msg("提示:请您选择文件!");
}
}
private bool ThumbnailCallback()
{
return false;
}