http://blog.csdn.net/wonsoft/archive/2009/04/29/4136711.aspx
ASP.NET(C#)图片加文字、图片水印
一、图片上加文字:
-
-
-
-
- private void AddTextToImg(string fileName,string text)
- {
- if(!File.Exists(MapPath(fileName)))
- {
- throw new FileNotFoundException("The file don't exist!");
- }
-
- if( text == string.Empty )
- {
- return;
- }
-
-
- System.Drawing.Image image = System.Drawing.Image.FromFile(MapPath(fileName));
- Bitmap bitmap = new Bitmap(image,image.Width,image.Height);
- Graphics g = Graphics.FromImage(bitmap);
-
- float fontSize = 12.0f;
- float textWidth = text.Length*fontSize;
-
- float rectX = 0;
- float rectY = 0;
- float rectWidth = text.Length*(fontSize+8);
- float rectHeight = fontSize+8;
-
- RectangleF textArea = new RectangleF(rectX,rectY,rectWidth,rectHeight);
-
- Font font = new Font("宋体",fontSize);
- Brush whiteBrush = new SolidBrush(Color.White);
- Brush blackBrush = new SolidBrush(Color.Black);
-
- g.FillRectangle(blackBrush,rectX,rectY,rectWidth,rectHeight);
-
- g.DrawString(text,font,whiteBrush,textArea);
- MemoryStream ms = new MemoryStream( );
-
- bitmap.Save(ms,ImageFormat.Jpeg);
-
-
- Response.Clear();
- Response.ContentType = "image/jpeg";
- Response.BinaryWrite( ms.ToArray() );
-
- g.Dispose();
- bitmap.Dispose();
- image.Dispose();
- }
二、图片上加水印:
-
-
-
-
-
-
-
-
-
-
-
-
-
- public 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;
- }
-
- string ModifyImagePath = BasePath + filePath;
- int lucencyPercent=25;
- Image modifyImage=null;
- Image drawedImage=null;
- Graphics g=null;
- try
- {
-
- modifyImage=Image.FromFile(ModifyImagePath,true);
- drawedImage=Image.FromFile(BasePath + 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/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;
- 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
- {
- }
- }
- }
四、C#图片水印生成类(图片、文字、透明水印)
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- using System;
- using System.Drawing;
- using System.Drawing.Imaging;
- using System.Drawing.Drawing2D;
- using System.IO;
-
-
-
- public enum ImagePosition
- {
- LeftTop,
- LeftBottom,
- RightTop,
- RigthBottom,
- TopMiddle,
- BottomMiddle,
- Center
- }
-
-
-
-
- public class WaterImageManage
- {
-
-
-
- public WaterImageManage ()
- {
-
-
-
- }
-
-
-
-
-
-
-
-
-
-
- public string DrawImage(string sourcePicture,
- string waterImage,
- float alpha,
- ImagePosition position,
- string PicturePath )
- {
-
-
-
- if (sourcePicture == string.Empty || waterImage == string.Empty || alpha == 0.0 || PicturePath == string.Empty)
- {
- return sourcePicture;
- }
-
-
-
-
- string sourcePictureName = PicturePath + sourcePicture;
- string waterPictureName = PicturePath + waterImage;
- string fileSourceExtension = System.IO.Path.GetExtension(sourcePictureName).ToLower();
- string fileWaterExtension = System.IO.Path.GetExtension(waterPictureName).ToLower();
-
-
-
- if (System.IO.File.Exists(sourcePictureName) == false ||
- System.IO.File.Exists(waterPictureName) == false ||(
- fileSourceExtension != ".gif" &&
- fileSourceExtension != ".jpg" &&
- fileSourceExtension != ".png") || (
- fileWaterExtension != ".gif" &&
- fileWaterExtension != ".jpg" &&
- fileWaterExtension != ".png")
- )
- {
- return sourcePicture;
- }
-
-
-
-
- string targetImage = sourcePictureName.Replace ( System.IO.Path.GetExtension(sourcePictureName),"") + "_1101.jpg";
-
-
-
-
- Image imgPhoto = Image.FromFile(sourcePictureName);
-
-
-
- int phWidth = imgPhoto.Width;
- int phHeight = imgPhoto.Height;
-
-
-
-
- Bitmap bmPhoto = new Bitmap(phWidth, phHeight, PixelFormat.Format24bppRgb);
-
-
-
-
- bmPhoto.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution);
-
-
-
-
- Graphics grPhoto = Graphics.FromImage(bmPhoto);
-
-
-
-
- Image imgWatermark = new Bitmap(waterPictureName);
-
-
-
-
- int wmWidth = imgWatermark.Width;
- int wmHeight = imgWatermark.Height;
-
-
-
-
-
-
-
-
-
- grPhoto.SmoothingMode = SmoothingMode.AntiAlias;
-
-
-
-
- grPhoto.DrawImage(imgPhoto,
- new Rectangle(0, 0, phWidth, phHeight),
- 0,
- 0,
- phWidth,
- phHeight,
- GraphicsUnit.Pixel);
-
-
-
-
- Bitmap bmWatermark = new Bitmap(bmPhoto);
- bmWatermark.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution);
-
-
-
-
- Graphics grWatermark = Graphics.FromImage(bmWatermark);
-
-
-
-
- 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);
-
-
-
-
- int xPosOfWm;
- int yPosOfWm;
-
- switch (position)
- {
- case ImagePosition .BottomMiddle :
- xPosOfWm = (phWidth-wmWidth ) / 2 ;
- yPosOfWm = phHeight- wmHeight -10;
- break ;
- case ImagePosition .Center :
- xPosOfWm = (phWidth - wmWidth) / 2;
- yPosOfWm = (phHeight-wmHeight ) / 2;
- break ;
- case ImagePosition .LeftBottom :
- xPosOfWm = 10;
- yPosOfWm = phHeight - wmHeight - 10;
- break ;
- case ImagePosition .LeftTop :
- xPosOfWm = 10;
- yPosOfWm = 10;
- break;
- case ImagePosition .RightTop :
- xPosOfWm = phWidth - wmWidth - 10;
- yPosOfWm = 10;
- break ;
- case ImagePosition .RigthBottom :
- xPosOfWm = phWidth - wmWidth - 10;
- yPosOfWm = phHeight - wmHeight - 10;
- break ;
- case ImagePosition.TopMiddle :
- xPosOfWm = (phWidth - wmWidth) / 2;
- yPosOfWm = 10;
- break ;
- default:
- xPosOfWm = 10;
- yPosOfWm = phHeight - wmHeight - 10;
- break;
- }
-
-
-
-
- grWatermark.DrawImage(imgWatermark,
- new Rectangle(xPosOfWm,
- yPosOfWm,
- wmWidth,
- wmHeight),
- 0,
- 0,
- wmWidth,
- wmHeight,
- GraphicsUnit.Pixel,
- imageAttributes);
-
-
- imgPhoto = bmWatermark;
- grPhoto.Dispose();
- grWatermark.Dispose();
-
-
-
-
- imgPhoto.Save(targetImage, ImageFormat.Jpeg);
- imgPhoto.Dispose();
- imgWatermark.Dispose();
- return targetImage.Replace (PicturePath,"");
- }
-
-
-
-
-
-
-
-
-
-
- public string DrawWords(string sourcePicture,
- string waterWords,
- float alpha,
- ImagePosition position,
- string PicturePath)
- {
-
-
-
- if (sourcePicture == string.Empty || waterWords == string.Empty || alpha == 0.0 || PicturePath == string.Empty)
- {
- return sourcePicture;
- }
-
-
-
-
- string sourcePictureName = PicturePath + sourcePicture;
- string fileExtension = System.IO.Path.GetExtension(sourcePictureName).ToLower();
-
-
-
-
- if (System.IO.File.Exists(sourcePictureName) == false || (
- fileExtension != ".gif" &&
- fileExtension != ".jpg" &&
- fileExtension != ".png" ))
- {
- return sourcePicture;
- }
-
-
-
-
- string targetImage = sourcePictureName.Replace(System.IO.Path.GetExtension(sourcePictureName), "") + "_0703.jpg";
-
-
- Image imgPhoto = Image.FromFile(sourcePictureName);
-
-
- int phWidth = imgPhoto.Width;
- int phHeight = imgPhoto.Height;
-
-
-
- Bitmap bmPhoto = new Bitmap(phWidth, phHeight, PixelFormat.Format24bppRgb);
-
-
-
- bmPhoto.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution);
-
-
- Graphics grPhoto = Graphics.FromImage(bmPhoto);
-
-
- grPhoto.SmoothingMode = SmoothingMode.AntiAlias;
-
-
- grPhoto.DrawImage(
- imgPhoto,
- new Rectangle(0, 0, phWidth, phHeight),
- 0,
- 0,
- phWidth,
- phHeight,
- GraphicsUnit.Pixel);
-
-
-
- int[] sizes = new int[] { 16, 14, 12, 10, 8, 6, 4 };
-
-
- Font crFont = null;
-
- SizeF crSize = new SizeF();
-
-
-
- for (int i = 0; i < 7; i++)
- {
- crFont = new Font("arial", sizes[i], FontStyle.Bold);
-
-
- crSize = grPhoto.MeasureString(waterWords, crFont);
-
-
- if ((ushort)crSize.Width < (ushort)phWidth)
- break;
- }
-
-
- int yPixlesFromBottom = (int)(phHeight * .05);
-
-
- float wmHeight = crSize.Height;
- float wmWidth = crSize .Width ;
-
- float xPosOfWm;
- float 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;
- yPosOfWm = phHeight - wmHeight - 10;
- 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;
- }
-
-
- StringFormat StrFormat = new StringFormat();
-
-
- StrFormat.Alignment = StringAlignment.Center;
-
-
-
- int m_alpha = Convert .ToInt32 ( 256 * alpha);
- SolidBrush semiTransBrush2 = new SolidBrush(Color.FromArgb(m_alpha, 0, 0, 0));
-
-
-
|
请发表评论