效果與商城上的效果類似
protected void Add_Click(object sender, EventArgs e)
{
DirectoryInfo theFolder = new DirectoryInfo(Request.PhysicalApplicationPath + "Images\\Product");
//周遊檔案夾
foreach (DirectoryInfo NextFolder in theFolder.GetDirectories())
{
foreach (FileInfo NextFile in NextFolder.GetFiles())
{
if (NextFile.Name.Contains("SY800SY"))
{
File.Delete(NextFile.FullName);
}
else if (!NextFile.Name.Contains("SY.") && !NextFile.Name.Contains("SY800."))
{
if (!NextFile.Name.Contains("47-47_") && !NextFile.Name.Contains("50-50_") &&
!NextFile.Name.Contains("80-80_") && !NextFile.Name.Contains("100-100_") &&
!NextFile.Name.Contains("120-120_") && !NextFile.Name.Contains("140-140_") &&
!NextFile.Name.Contains("160-160_") && !NextFile.Name.Contains("380-380_"))
{
MarkWater(NextFile.FullName, Request.PhysicalApplicationPath + "images\\" + "logo-back.png");
}
}
}
}
}
給圖檔添加水印的方法
/// <summary>
/// 給圖檔添加水印
/// </summary>
/// <param name="filePath">原圖檔位址</param>
/// <param name="waterFile">水印圖檔位址</param>
private void MarkWater(string filePath, string waterFile)
{
int i = filePath.LastIndexOf(".");
string ex = filePath.Substring(i, filePath.Length - i);
if (string.Compare(ex, ".gif", true) == 0)
{
return;
}
int newp = filePath.LastIndexOf(".");
string newpo = filePath.Substring(0, newp);
string newpolast = filePath.Substring(newp + 1);
string newlastimg = newpo + "SY800" + "." + newpolast;
BitmapHelper.MakeThumbnail(filePath, newlastimg, 800, 800, "DB", "JPG");
string ModifyImagePath = newlastimg;
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(waterFile, true);
g = Graphics.FromImage(modifyImage);
int x = (modifyImage.Width - drawedImage.Width) / 2;
int y = (modifyImage.Height - drawedImage.Height) / 2;
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/100f, 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), 10, 10,
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;
fs = new FileStream(ModifyImagePath.Replace("SY800.", "SY."), FileMode.Create, FileAccess.Write);
if (fs != null)
{
fs.Write(imgData, 0, imgData.Length);
fs.Close();
}
}
finally
{
try
{
drawedImage.Dispose();
modifyImage.Dispose();
g.Dispose();
}
catch
{
}
}
}
用到的一個生成縮略圖的類
public class BitmapHelper
{
/// <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)
{
Image originalImage = 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圖檔
Image bitmap = new Bitmap(towidth, toheight);
//建立一個畫闆
Graphics g = Graphics.FromImage(bitmap);
//設定高品質插值法
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
//設定高品質,低速度呈現平滑程度
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
//清空畫布并以透明背景色填充
g.Clear(Color.Transparent);
//在指定位置并且按指定大小繪制原圖檔的指定部分
g.DrawImage(originalImage, new Rectangle(0, 0, towidth, toheight),
new Rectangle(x, y, ow, oh),
GraphicsUnit.Pixel);
try
{
//儲存縮略圖
if (type.ToUpper() == "JPG")
{
bitmap.Save(thumbnailPath, System.Drawing.Imaging.ImageFormat.Jpeg);
}
if (type.ToUpper() == "BMP")
{
bitmap.Save(thumbnailPath, System.Drawing.Imaging.ImageFormat.Bmp);
}
if (type.ToUpper() == "GIF")
{
bitmap.Save(thumbnailPath, System.Drawing.Imaging.ImageFormat.Gif);
}
if (type.ToUpper() == "PNG")
{
bitmap.Save(thumbnailPath, System.Drawing.Imaging.ImageFormat.Png);
}
}
catch (System.Exception e)
{
throw e;
}
finally
{
originalImage.Dispose();
bitmap.Dispose();
g.Dispose();
}
}
}
基本上就Ok 了 自己記錄的文章沒有仔細排版 也沒有多少文字說明