public enum ImagePosition
{
/**/
/// < summary>
/// 左上
/// < /summary>
LeftTop,
/**/
/// < summary>
/// 左下
/// < /summary>
LeftBottom,
/**/
/// < summary>
/// 右上
/// < /summary>
RightTop,
/**/
/// < summary>
/// 右下
/// < /summary>
RigthBottom,
/**/
/// < summary>
/// 顶部居中
/// < /summary>
TopMiddle,
/**/
/// < summary>
/// 底部居中
/// < /summary>
BottomMiddle,
/**/
/// < summary>
/// 中心
/// < /summary>
Center
}
#region 将byte转换成MemoryStream类型
/**/
/// < summary>
/// ASP.NET图片加水印:将byte转换成MemoryStream类型
/// < /summary>
/// < param name="mybyte">byte[]变量< /param>
/// < returns>< /returns>
private MemoryStream ByteToStream(byte[] mybyte)
{
MemoryStream mymemorystream = new MemoryStream(mybyte, 0, mybyte.Length);
return mymemorystream;
}
#endregion
#region 将byte转换成Image文件
/**/
/// < summary>
/// ASP.NET图片加水印:将byte转换成Image文件
/// < /summary>
/// < param name="mybyte">byte[]变量< /param>
/// < returns>< /returns>
private System.Drawing.Image SetByteToImage(byte[] mybyte)
{
System.Drawing.Image image;
MemoryStream mymemorystream = new MemoryStream(mybyte, 0, mybyte.Length);
image = System.Drawing.Image.FromStream(mymemorystream);
return image;
}
#endregion
#region 将文件转换成流
//public byte[] SetImageToByteArray(string fileName, ref string fileSize)
/**/
/// < summary>
/// 将文件转换成流
/// < /summary>
/// < param name="fileName">文件全路径< /param>
/// < returns>< /returns>
private byte[] SetImageToByteArray(string fileName)
{
byte[] image = null;
try
{
FileStream fs = new FileStream(fileName, FileMode.Open);
FileInfo fileInfo = new FileInfo(fileName);
//fileSize = Convert.ToDecimal(fileInfo.Length / 1024).ToString("f2") + " K";
int streamLength = (int)fs.Length;
image = new byte[streamLength];
fs.Read(image, 0, streamLength);
fs.Close();
return image;
}
catch
{
return image;
}
}
#endregion
//将image转化为二进制
public byte[] GetByteImage(Image img)
{
byte[] bt = null;
if (!img.Equals(null))
{
using (MemoryStream mostream = new MemoryStream())
{
Bitmap bmp = new Bitmap(img);
bmp.Save(mostream, System.Drawing.Imaging.ImageFormat.Bmp);//将图像以指定的格式存入缓存内存流
bt = new byte[mostream.Length];
mostream.Position = 0;//设置留的初始位置
mostream.Read(bt, 0, Convert.ToInt32(bt.Length));
}
}
return bt;
}
/// 将 Stream 转成 byte[]
public byte[] StreamToBytes(Stream stream)
{
byte[] bytes = new byte[stream.Length];
stream.Read(bytes, 0, bytes.Length);
// 设置当前流的位置为流的开始
stream.Seek(0, SeekOrigin.Begin);
return bytes;
}
/// < summary>
/// ASP.NET图片加水印:在图片上添加透明水印图片
/// < /summary>
/// < param name="sourcePicture">原来图片地址(路径+文件名)< /param>
/// < param name="waterPicture">需要添加到图片上的图片< /param>
/// < param name="alpha">透明度(0.1~1.0之间)< /param>
/// < param name="fRewrite">是否覆盖原图片(如果不覆盖,那么将在同目录下生成一个文件名带0607的文件)< /param>
/// < returns>< /returns>
public byte[] DrawWaterImages(string sourcePicture, Image waterPicture, float alpha, ImagePosition position)
{
if (!System.IO.File.Exists(sourcePicture))
{
return new byte[] { };
}
string fileExtension = System.IO.Path.GetExtension(sourcePicture).ToLower();
if (fileExtension != ".gif" && fileExtension != ".jpg" && fileExtension != ".png" && fileExtension != ".bmp")
{
return new byte[] { };
}
Image imgPhoto = null;
Bitmap bmPhoto = null;
Graphics grPhoto = null;
try
{
//创建一个图片对象用来装载要被添加水印的图片
imgPhoto = Image.FromStream(ByteToStream(SetImageToByteArray(sourcePicture)));
//获取图片的宽和高
int phWidth = imgPhoto.Width;
int phHeight = imgPhoto.Height;
//建立一个bitmap,和我们需要加水印的图片一样大小
bmPhoto = new Bitmap(phWidth, phHeight, PixelFormat.Format24bppRgb);
//SetResolution:设置此 Bitmap 的分辨率
//这里直接将我们需要添加水印的图片的分辨率赋给了bitmap
bmPhoto.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution);
//Graphics:封装一个 GDI+ 绘图图面。
grPhoto = Graphics.FromImage(bmPhoto);
//设置图形的品质
grPhoto.SmoothingMode = SmoothingMode.AntiAlias;
//将我们要添加水印的图片按照原始大小描绘(复制)到图形中
grPhoto.DrawImage(
imgPhoto, // 要添加水印的图片
new Rectangle(0, 0, phWidth, phHeight), // 根据要添加的水印图片的宽和高
0, // X方向从0点开始描绘
0, // Y方向
phWidth, // X方向描绘长度
phHeight, // Y方向描绘长度
GraphicsUnit.Pixel); // 描绘的单位,这里用的是像素
/
waterPicture = MakeThumbnailFromImage(waterPicture, Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings["WaterImgWidth"]), Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings["WaterImgHeight"]), "EQ");
System.Drawing.Image copyImage = waterPicture;
int wmHeight = copyImage.Height;
int wmWidth = copyImage.Width;
int xPosOfWm;
int yPosOfWm;
switch (position)
{
case ImagePosition.BottomMiddle:
xPosOfWm = phWidth / 2;
yPosOfWm = phHeight - wmHeight - 10;
break;
case ImagePosition.Center:
xPosOfWm = phWidth / 2;
yPosOfWm = phHeight / 2;
break;
case ImagePosition.LeftBottom:
//xPosOfWm = wmWidth-93;
//yPosOfWm = phHeight - wmHeight - 10-68;
xPosOfWm = Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings["WaterImgX"]);
yPosOfWm = Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings["WaterImgY"]);
break;
case ImagePosition.LeftTop:
xPosOfWm = wmWidth / 2;
yPosOfWm = wmHeight / 2;
break;
case ImagePosition.RightTop:
xPosOfWm = phWidth - wmWidth - 10;
yPosOfWm = wmHeight;
break;
case ImagePosition.RigthBottom:
xPosOfWm = phWidth - wmWidth - 10;
yPosOfWm = phHeight - wmHeight - 10;
break;
case ImagePosition.TopMiddle:
xPosOfWm = phWidth / 2;
yPosOfWm = wmWidth;
break;
default:
xPosOfWm = wmWidth;
yPosOfWm = phHeight - wmHeight - 10;
break;
}
/
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, alpha, 0.0f},
new float[] {0.0f, 0.0f, 0.0f, 0.0f, 1.0f}
};
ColorMatrix wmColorMatrix = new ColorMatrix(colorMatrixElements);
imageAttributes.SetColorMatrix(wmColorMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
/
grPhoto.DrawImage(copyImage, new Rectangle(xPosOfWm, yPosOfWm,
copyImage.Width, copyImage.Height), 0, 0, copyImage.Width, copyImage.Height, GraphicsUnit.Pixel, imageAttributes);
//
imgPhoto = bmPhoto;
//释放资源,将定义的Graphics实例grPhoto释放,grPhoto功德圆满
grPhoto.Dispose();
MemoryStream tempStream = new MemoryStream();
imgPhoto.Save(tempStream, System.Drawing.Imaging.ImageFormat.Jpeg);
//Image转为byte[]
//byte[] imgbyteArr = GetByteImage(imgPhoto);
tempStream.Position = 0;
byte[] imgbyteArr = StreamToBytes(tempStream);
tempStream.Close();
tempStream.Dispose();
return imgbyteArr;
}
catch (Exception ex)
{
Log4NetHelper.Log.Error(ex);
return new byte[]{};
}
finally
{
if (imgPhoto != null)
imgPhoto.Dispose();
if (bmPhoto != null)
bmPhoto.Dispose();
if (grPhoto != null)
grPhoto.Dispose();
if (waterPicture != null)
waterPicture.Dispose();
}
}
public static Image GetQRCode(string url)
{
QRCodeEncoder qrCodeEncoder = new QRCodeEncoder();
qrCodeEncoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.BYTE;
qrCodeEncoder.QRCodeScale = 4;
qrCodeEncoder.QRCodeVersion = 5;
qrCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.M;
System.Drawing.Image image = qrCodeEncoder.Encode(url);
return image;
}
{
/**/
/// < summary>
/// 左上
/// < /summary>
LeftTop,
/**/
/// < summary>
/// 左下
/// < /summary>
LeftBottom,
/**/
/// < summary>
/// 右上
/// < /summary>
RightTop,
/**/
/// < summary>
/// 右下
/// < /summary>
RigthBottom,
/**/
/// < summary>
/// 顶部居中
/// < /summary>
TopMiddle,
/**/
/// < summary>
/// 底部居中
/// < /summary>
BottomMiddle,
/**/
/// < summary>
/// 中心
/// < /summary>
Center
}
#region 将byte转换成MemoryStream类型
/**/
/// < summary>
/// ASP.NET图片加水印:将byte转换成MemoryStream类型
/// < /summary>
/// < param name="mybyte">byte[]变量< /param>
/// < returns>< /returns>
private MemoryStream ByteToStream(byte[] mybyte)
{
MemoryStream mymemorystream = new MemoryStream(mybyte, 0, mybyte.Length);
return mymemorystream;
}
#endregion
#region 将byte转换成Image文件
/**/
/// < summary>
/// ASP.NET图片加水印:将byte转换成Image文件
/// < /summary>
/// < param name="mybyte">byte[]变量< /param>
/// < returns>< /returns>
private System.Drawing.Image SetByteToImage(byte[] mybyte)
{
System.Drawing.Image image;
MemoryStream mymemorystream = new MemoryStream(mybyte, 0, mybyte.Length);
image = System.Drawing.Image.FromStream(mymemorystream);
return image;
}
#endregion
#region 将文件转换成流
//public byte[] SetImageToByteArray(string fileName, ref string fileSize)
/**/
/// < summary>
/// 将文件转换成流
/// < /summary>
/// < param name="fileName">文件全路径< /param>
/// < returns>< /returns>
private byte[] SetImageToByteArray(string fileName)
{
byte[] image = null;
try
{
FileStream fs = new FileStream(fileName, FileMode.Open);
FileInfo fileInfo = new FileInfo(fileName);
//fileSize = Convert.ToDecimal(fileInfo.Length / 1024).ToString("f2") + " K";
int streamLength = (int)fs.Length;
image = new byte[streamLength];
fs.Read(image, 0, streamLength);
fs.Close();
return image;
}
catch
{
return image;
}
}
#endregion
//将image转化为二进制
public byte[] GetByteImage(Image img)
{
byte[] bt = null;
if (!img.Equals(null))
{
using (MemoryStream mostream = new MemoryStream())
{
Bitmap bmp = new Bitmap(img);
bmp.Save(mostream, System.Drawing.Imaging.ImageFormat.Bmp);//将图像以指定的格式存入缓存内存流
bt = new byte[mostream.Length];
mostream.Position = 0;//设置留的初始位置
mostream.Read(bt, 0, Convert.ToInt32(bt.Length));
}
}
return bt;
}
/// 将 Stream 转成 byte[]
public byte[] StreamToBytes(Stream stream)
{
byte[] bytes = new byte[stream.Length];
stream.Read(bytes, 0, bytes.Length);
// 设置当前流的位置为流的开始
stream.Seek(0, SeekOrigin.Begin);
return bytes;
}
/// < summary>
/// ASP.NET图片加水印:在图片上添加透明水印图片
/// < /summary>
/// < param name="sourcePicture">原来图片地址(路径+文件名)< /param>
/// < param name="waterPicture">需要添加到图片上的图片< /param>
/// < param name="alpha">透明度(0.1~1.0之间)< /param>
/// < param name="fRewrite">是否覆盖原图片(如果不覆盖,那么将在同目录下生成一个文件名带0607的文件)< /param>
/// < returns>< /returns>
public byte[] DrawWaterImages(string sourcePicture, Image waterPicture, float alpha, ImagePosition position)
{
if (!System.IO.File.Exists(sourcePicture))
{
return new byte[] { };
}
string fileExtension = System.IO.Path.GetExtension(sourcePicture).ToLower();
if (fileExtension != ".gif" && fileExtension != ".jpg" && fileExtension != ".png" && fileExtension != ".bmp")
{
return new byte[] { };
}
Image imgPhoto = null;
Bitmap bmPhoto = null;
Graphics grPhoto = null;
try
{
//创建一个图片对象用来装载要被添加水印的图片
imgPhoto = Image.FromStream(ByteToStream(SetImageToByteArray(sourcePicture)));
//获取图片的宽和高
int phWidth = imgPhoto.Width;
int phHeight = imgPhoto.Height;
//建立一个bitmap,和我们需要加水印的图片一样大小
bmPhoto = new Bitmap(phWidth, phHeight, PixelFormat.Format24bppRgb);
//SetResolution:设置此 Bitmap 的分辨率
//这里直接将我们需要添加水印的图片的分辨率赋给了bitmap
bmPhoto.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution);
//Graphics:封装一个 GDI+ 绘图图面。
grPhoto = Graphics.FromImage(bmPhoto);
//设置图形的品质
grPhoto.SmoothingMode = SmoothingMode.AntiAlias;
//将我们要添加水印的图片按照原始大小描绘(复制)到图形中
grPhoto.DrawImage(
imgPhoto, // 要添加水印的图片
new Rectangle(0, 0, phWidth, phHeight), // 根据要添加的水印图片的宽和高
0, // X方向从0点开始描绘
0, // Y方向
phWidth, // X方向描绘长度
phHeight, // Y方向描绘长度
GraphicsUnit.Pixel); // 描绘的单位,这里用的是像素
/
waterPicture = MakeThumbnailFromImage(waterPicture, Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings["WaterImgWidth"]), Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings["WaterImgHeight"]), "EQ");
System.Drawing.Image copyImage = waterPicture;
int wmHeight = copyImage.Height;
int wmWidth = copyImage.Width;
int xPosOfWm;
int yPosOfWm;
switch (position)
{
case ImagePosition.BottomMiddle:
xPosOfWm = phWidth / 2;
yPosOfWm = phHeight - wmHeight - 10;
break;
case ImagePosition.Center:
xPosOfWm = phWidth / 2;
yPosOfWm = phHeight / 2;
break;
case ImagePosition.LeftBottom:
//xPosOfWm = wmWidth-93;
//yPosOfWm = phHeight - wmHeight - 10-68;
xPosOfWm = Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings["WaterImgX"]);
yPosOfWm = Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings["WaterImgY"]);
break;
case ImagePosition.LeftTop:
xPosOfWm = wmWidth / 2;
yPosOfWm = wmHeight / 2;
break;
case ImagePosition.RightTop:
xPosOfWm = phWidth - wmWidth - 10;
yPosOfWm = wmHeight;
break;
case ImagePosition.RigthBottom:
xPosOfWm = phWidth - wmWidth - 10;
yPosOfWm = phHeight - wmHeight - 10;
break;
case ImagePosition.TopMiddle:
xPosOfWm = phWidth / 2;
yPosOfWm = wmWidth;
break;
default:
xPosOfWm = wmWidth;
yPosOfWm = phHeight - wmHeight - 10;
break;
}
/
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, alpha, 0.0f},
new float[] {0.0f, 0.0f, 0.0f, 0.0f, 1.0f}
};
ColorMatrix wmColorMatrix = new ColorMatrix(colorMatrixElements);
imageAttributes.SetColorMatrix(wmColorMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
/
grPhoto.DrawImage(copyImage, new Rectangle(xPosOfWm, yPosOfWm,
copyImage.Width, copyImage.Height), 0, 0, copyImage.Width, copyImage.Height, GraphicsUnit.Pixel, imageAttributes);
//
imgPhoto = bmPhoto;
//释放资源,将定义的Graphics实例grPhoto释放,grPhoto功德圆满
grPhoto.Dispose();
MemoryStream tempStream = new MemoryStream();
imgPhoto.Save(tempStream, System.Drawing.Imaging.ImageFormat.Jpeg);
//Image转为byte[]
//byte[] imgbyteArr = GetByteImage(imgPhoto);
tempStream.Position = 0;
byte[] imgbyteArr = StreamToBytes(tempStream);
tempStream.Close();
tempStream.Dispose();
return imgbyteArr;
}
catch (Exception ex)
{
Log4NetHelper.Log.Error(ex);
return new byte[]{};
}
finally
{
if (imgPhoto != null)
imgPhoto.Dispose();
if (bmPhoto != null)
bmPhoto.Dispose();
if (grPhoto != null)
grPhoto.Dispose();
if (waterPicture != null)
waterPicture.Dispose();
}
}
public static Image GetQRCode(string url)
{
QRCodeEncoder qrCodeEncoder = new QRCodeEncoder();
qrCodeEncoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.BYTE;
qrCodeEncoder.QRCodeScale = 4;
qrCodeEncoder.QRCodeVersion = 5;
qrCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.M;
System.Drawing.Image image = qrCodeEncoder.Encode(url);
return image;
}