📄 webfunction.cs
字号:
Bitmap b = new Bitmap(image.Width, image.Height);
try
{
Graphics g = Graphics.FromImage(b);
g.Clear(Color.White);
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
g.DrawImage(image, 0, 0, image.Width, image.Height);
if (ShowWatermark)
{
switch (WatermarkType)
{
//是图片的话
case "I":
addWatermarkImage(g, System.Web.HttpContext.Current.Server.MapPath(Watermarkimgpath), WatermarkPosition, image.Width, image.Height);
break;
//如果是文字
case "T":
WatermarkText += System.Configuration.ConfigurationManager.AppSettings["WaterImgText"].ToString();
addWatermarkText(g, WatermarkText, WatermarkPosition, image.Width, image.Height);
break;
}
}
}
catch (Exception e)
{
if (File.Exists(oldpath))
{
File.Delete(oldpath);
}
}
finally
{
b.Save(newpath, System.Drawing.Imaging.ImageFormat.Jpeg);
b.Dispose();
image.Dispose();
if (File.Exists(oldpath))
{
File.Delete(oldpath);
}
}
}
/// <summary>
/// 加水印文字
/// </summary>
/// <param name="picture">imge 对象</param>
/// <param name="_watermarkText">水印文字内容</param>
/// <param name="_watermarkPosition">水印位置 TL TR BR BL</param>
/// <param name="_width">被加水印图片的宽</param>
/// <param name="_height">被加水印图片的高</param>
public void addWatermarkText(Graphics picture, string _watermarkText, string _watermarkPosition, int _width, int _height)
{
int[] sizes = new int[] { 12, 10, 8, 6, 4 };
Font crFont = null;
SizeF crSize = new SizeF();
for (int n = 0; n < sizes.Length; n++)
{
crFont = new Font("arial", sizes[n], FontStyle.Bold);
crSize = picture.MeasureString(_watermarkText, crFont);
if ((ushort)crSize.Width < (ushort)_width)
break;
}
float xpos = 0;
float ypos = 0;
switch (_watermarkPosition)
{
case "TL":
xpos = ((float)_width * (float).01) + (crSize.Width / 2);
ypos = (float)_height * (float).01;
break;
case "TR":
xpos = ((float)_width * (float).99) - (crSize.Width / 2);
ypos = (float)_height * (float).01;
break;
case "BR":
xpos = ((float)_width * (float).99) - (crSize.Width / 2);
ypos = ((float)_height * (float).99) - crSize.Height;
break;
case "BL":
xpos = ((float)_width * (float).01) + (crSize.Width / 2);
ypos = ((float)_height * (float).99) - crSize.Height;
break;
}
StringFormat StrFormat = new StringFormat();
StrFormat.Alignment = StringAlignment.Center;
SolidBrush semiTransBrush2 = new SolidBrush(Color.FromArgb(153, 0, 0, 0));
picture.DrawString(_watermarkText, crFont, semiTransBrush2, xpos + 1, ypos + 1, StrFormat);
SolidBrush semiTransBrush = new SolidBrush(Color.FromArgb(153, 255, 255, 255));
picture.DrawString(_watermarkText, crFont, semiTransBrush, xpos, ypos, StrFormat);
semiTransBrush2.Dispose();
semiTransBrush.Dispose();
}
/**/
/// <summary>
/// 加水印图片
/// </summary>
/// <param name="picture">imge 对象</param>
/// <param name="WaterMarkPicPath">水印图片的地址</param>
/// <param name="_watermarkPosition">水印位置 TL TR BL BR</param>
/// <param name="_width">被加水印图片的宽</param>
/// <param name="_height">被加水印图片的高</param>
public void addWatermarkImage(Graphics picture, string WaterMarkPicPath, string _watermarkPosition, int _width, int _height)
{
System.Drawing.Image watermark = new Bitmap(WaterMarkPicPath);
ImageAttributes imageAttributes = new ImageAttributes();
ColorMap colorMap = new ColorMap();
colorMap.OldColor = Color.FromArgb(255, 0, 255, 0);
colorMap.NewColor = Color.FromArgb(0, 0, 0, 0);
ColorMap[] remapTable = { colorMap };
imageAttributes.SetRemapTable(remapTable, ColorAdjustType.Bitmap);
float[][] colorMatrixElements = {
new float[] {1.0f, 0.0f, 0.0f, 0.0f, 0.0f},
new float[] {0.0f, 1.0f, 0.0f, 0.0f, 0.0f},
new float[] {0.0f, 0.0f, 1.0f, 0.0f, 0.0f},
new float[] {0.0f, 0.0f, 0.0f, 0.3f, 0.0f},
new float[] {0.0f, 0.0f, 0.0f, 0.0f, 1.0f}
};
ColorMatrix colorMatrix = new ColorMatrix(colorMatrixElements);
imageAttributes.SetColorMatrix(colorMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
int xpos = 0;
int ypos = 0;
int WatermarkWidth = 0;
int WatermarkHeight = 0;
double bl = 1d;
//计算水印图片的比率
//取背景的1/4宽度来比较
if ((_width > watermark.Width * 4) && (_height > watermark.Height * 4))
{
bl = 1;
}
else if ((_width > watermark.Width * 4) && (_height < watermark.Height * 4))
{
bl = Convert.ToDouble(_height / 4) / Convert.ToDouble(watermark.Height);
}
else
if ((_width < watermark.Width * 4) && (_height > watermark.Height * 4))
{
bl = Convert.ToDouble(_width / 4) / Convert.ToDouble(watermark.Width);
}
else
{
if ((_width * watermark.Height) > (_height * watermark.Width))
{
bl = Convert.ToDouble(_height / 4) / Convert.ToDouble(watermark.Height);
}
else
{
bl = Convert.ToDouble(_width / 4) / Convert.ToDouble(watermark.Width);
}
}
WatermarkWidth = Convert.ToInt32(watermark.Width * bl);
WatermarkHeight = Convert.ToInt32(watermark.Height * bl);
switch (_watermarkPosition)
{
case "TL":
xpos = 10;
ypos = 10;
break;
case "TR":
xpos = _width - WatermarkWidth - 10;
ypos = 10;
break;
case "BR":
xpos = _width - WatermarkWidth - 10;
ypos = _height - WatermarkHeight - 10;
break;
case "BL":
xpos = 10;
ypos = _height - WatermarkHeight - 10;
break;
}
picture.DrawImage(watermark, new Rectangle(xpos, ypos, WatermarkWidth, WatermarkHeight), 0, 0, watermark.Width, watermark.Height, GraphicsUnit.Pixel, imageAttributes);
watermark.Dispose();
imageAttributes.Dispose();
}
/**/
/// <summary>
/// 生成缩略图
/// </summary>
/// <param name="originalImagePath">原图片地址</param>
/// <param name="thumbnailPath">新图片地址</param>
/// <param name="tWidth">缩略图的宽</param>
/// <param name="tHeight">缩略图的高</param>
/// <param name="Mode">W H Cut</param>
public void GreateMiniImage(string originalImagePath, string thumbnailPath, int width, int height, string Mode)
{
System.Drawing.Image originalImage = null;
System.Drawing.Image bitmap = null;
try
{
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;
default:
break;
}
//新建一个bmp图片
bitmap = new System.Drawing.Bitmap(towidth, toheight);
//新建一个画板
Graphics g = System.Drawing.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);
//以jpg格式保存缩略图
bitmap.Save(thumbnailPath, System.Drawing.Imaging.ImageFormat.Jpeg);
}
catch (Exception e)
{
LeeHom.Error.Record("生成缩略图", e.ToString());
}
finally
{
originalImage.Dispose();
bitmap.Dispose();
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -