1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > c# 添加图片水印 可以指定水印位置+生成缩略图

c# 添加图片水印 可以指定水印位置+生成缩略图

时间:2024-04-14 02:48:07

相关推荐

c# 添加图片水印 可以指定水印位置+生成缩略图

c# 添加图片水印,可以指定水印位置+生成缩略图

图片上传函数,进行判断是否加水印,做出两种处理方式:

/**////<summary>

///上传图片代码

///</summary>

///<paramname="image_file">HtmlInputFile控件</param>

///<paramname="ImgPath">存放的文件夹绝对位置</param>

///<paramname="ImgLink">生成的图片的名称带后缀</param>

///<returns></returns>

publicboolUpPic(System.Web.UI.HtmlControls.HtmlInputFileimage_file,stringImgPath,stringImgLink)

{

if(image_file.PostedFile.FileName!=null&&image_file.PostedFile.FileName.Trim()!="")

{

try

{

if(!System.IO.Directory.Exists(ImgPath))

{

System.IO.Directory.CreateDirectory(ImgPath);

}

//生成缩略图

this.GreateMiniImage((ImgPath+ "\\"+"old_"+ImgLink),(ImgPath+ "\\"+"mini_"+ImgLink),50,50);

//如果显示水印

if(ShowWatermark)

{

image_file.PostedFile.SaveAs(ImgPath+"\\"+"old_"+ImgLink);

//加水印

this.addWaterMark((ImgPath+"\\"+"old_"+ImgLink),(ImgPath+"\\"+ImgLink));

}

else

{

image_file.PostedFile.SaveAs(ImgPath+"\\"+ImgLink);

}

returntrue;

}

catch

{

returnfalse;

}

}

else

{

returnfalse;

}

}

加水印的函数如下:

填加图片函数,需要下面两个函数的支持,当然也可以写到一起,不过那看起来就很冗长了。

/**////<summary>

///添加图片水印

///</summary>

///<paramname="oldpath">原图片绝对地址</param>

///<paramname="newpath">新图片放置的绝对地址</param>

privatevoidaddWaterMark(stringoldpath,stringnewpath)

{

try

{

System.Drawing.Imageimage=System.Drawing.Image.FromFile(oldpath);

Bitmapb=newBitmap(image.Width,image.Height,PixelFormat.Format24bppRgb);

Graphicsg=Graphics.FromImage(b);

g.Clear(Color.White);

g.SmoothingMode=SmoothingMode.HighQuality;

g.InterpolationMode=InterpolationMode.High;

g.DrawImage(image,0,0,image.Width,image.Height);

if(如果需要填加水印)

{

switch(水印类型)

{

//是图片的话

case"WM_IMAGE":

this.addWatermarkImage(g,Page.Server.MapPath(Watermarkimgpath),WatermarkPosition,image.Width,image.Height);

break;

//如果是文字

case"WM_TEXT":

this.addWatermarkText(g,WatermarkText,WatermarkPosition

,image.Width,image.Height);

break;

}

b.Save(newpath);

b.Dispose();

image.Dispose();

}

}

catch

{

if(File.Exists(oldpath))

{

File.Delete(oldpath);

}

}

finally

{

if(File.Exists(oldpath))

{

File.Delete(oldpath);

}

}

}

加水印文字 /**////<summary>

///加水印文字

///</summary>

///<paramname="picture">imge对象</param>

///<paramname="_watermarkText">水印文字内容</param>

///<paramname="_watermarkPosition">水印位置</param>

///<paramname="_width">被加水印图片的宽</param>

///<paramname="_height">被加水印图片的高</param>

privatevoidaddWatermarkText(Graphicspicture,string_watermarkText,string_watermarkPosition,int_width,int_height)

{

int[]sizes=newint[]{16,14,12,10,8,6,4};

FontcrFont=null;

SizeFcrSize=newSizeF();

for(inti=0;i<7;i++)

{

crFont=newFont("arial",sizes[i],FontStyle.Bold);

crSize=picture.MeasureString(_watermarkText,crFont);

if((ushort)crSize.Width<(ushort)_width)

break;

}

floatxpos=0;

floatypos=0;

switch(_watermarkPosition)

{

case"WM_TOP_LEFT":

xpos=((float)_width*(float).01)+(crSize.Width/2);

ypos=(float)_height*(float).01;

break;

case"WM_TOP_RIGHT":

xpos=((float)_width*(float).99)-(crSize.Width/2);

ypos=(float)_height*(float).01;

break;

case"WM_BOTTOM_RIGHT":

xpos=((float)_width*(float).99)-(crSize.Width/2);

ypos=((float)_height*(float).99)-crSize.Height;

break;

case"WM_BOTTOM_LEFT":

xpos=((float)_width*(float).01)+(crSize.Width/2);

ypos=((float)_height*(float).99)-crSize.Height;

break;

}

StringFormatStrFormat=newStringFormat();

StrFormat.Alignment=StringAlignment.Center;

SolidBrushsemiTransBrush2=newSolidBrush(Color.FromArgb(153,0,0,0));

picture.DrawString(_watermarkText,crFont,semiTransBrush2,xpos+1,ypos+1,StrFormat);

SolidBrushsemiTransBrush=newSolidBrush(Color.FromArgb(153,255,255,255));

picture.DrawString(_watermarkText,crFont,semiTransBrush,xpos,ypos,StrFormat);

semiTransBrush2.Dispose();

semiTransBrush.Dispose();

}

//代码已经修改,可以按比率还填加图片水印,不过如果背景图片和水印图片太不成比率的话(指水印图片要大于背景图片的1/4),出来的效果不是很好。

水印图片/**////<summary>

///加水印图片

///</summary>

///<paramname="picture">imge对象</param>

///<paramname="WaterMarkPicPath">水印图片的地址</param>

///<paramname="_watermarkPosition">水印位置</param>

///<paramname="_width">被加水印图片的宽</param>

///<paramname="_height">被加水印图片的高</param>

privatevoidaddWatermarkImage(Graphicspicture,stringWaterMarkPicPath,string_watermarkPosition,int_width,int_height)

{

Imagewatermark=newBitmap(WaterMarkPicPath);

ImageAttributesimageAttributes=newImageAttributes();

ColorMapcolorMap=newColorMap();

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={

newfloat[]{1.0f,0.0f,0.0f,0.0f,0.0f},

newfloat[]{0.0f,1.0f,0.0f,0.0f,0.0f},

newfloat[]{0.0f,0.0f,1.0f,0.0f,0.0f},

newfloat[]{0.0f,0.0f,0.0f,0.3f,0.0f},

newfloat[]{0.0f,0.0f,0.0f,0.0f,1.0f}

};

ColorMatrixcolorMatrix=newColorMatrix(colorMatrixElements);

imageAttributes.SetColorMatrix(colorMatrix,ColorMatrixFlag.Default,ColorAdjustType.Bitmap);

intxpos=0;

intypos=0;

intWatermarkWidth=0;

intWatermarkHeight=0;

doublebl=1d;

//计算水印图片的比率

//取背景的1/4宽度来比较

if((_width>watermark.Width*4)&&(_height>watermark.Height*4))

{

bl=1;

}

elseif((_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"WM_TOP_LEFT":

xpos=10;

ypos=10;

break;

case"WM_TOP_RIGHT":

xpos=_width-WatermarkWidth-10;

ypos=10;

break;

case"WM_BOTTOM_RIGHT":

xpos=_width-WatermarkWidth-10;

ypos=_height-WatermarkHeight-10;

break;

case"WM_BOTTOM_LEFT":

xpos=10;

ypos=_height-WatermarkHeight-10;

break;

}

picture.DrawImage(watermark,newRectangle(xpos,ypos,WatermarkWidth,WatermarkHeight),0,0,watermark.Width,watermark.Height,GraphicsUnit.Pixel,imageAttributes);

watermark.Dispose();

imageAttributes.Dispose();

}

生成缩略图函数

/**////<summary>

///生成缩略图

///</summary>

///<paramname="oldpath">原图片地址</param>

///<paramname="newpath">新图片地址</param>

///<paramname="tWidth">缩略图的宽</param>

///<paramname="tHeight">缩略图的高</param>

privatevoidGreateMiniImage(stringoldpath,stringnewpath,inttWidth,inttHeight)

{

try

{

System.Drawing.Imageimage=System.Drawing.Image.FromFile(oldpath);

doublebl=1d;

if((image.Width<=image.Height)&&(tWidth>=tHeight))

{

bl=Convert.ToDouble(image.Height)/Convert.ToDouble(tHeight);

}

elseif((image.Width>image.Height)&&(tWidth<tHeight))

{

bl=Convert.ToDouble(image.Width)/Convert.ToDouble(tWidth);

}

else

if((image.Width<=image.Height)&&(tWidth<=tHeight))

{

if(image.Height/tHeight>=image.Width/tWidth)

{

bl=Convert.ToDouble(image.Width)/Convert.ToDouble(tWidth);

}

else

{

bl=Convert.ToDouble(image.Height)/Convert.ToDouble(tHeight);

}

}

else

{

if(image.Height/tHeight>=image.Width/tWidth)

{

bl=Convert.ToDouble(image.Height)/Convert.ToDouble(tHeight);

}

else

{

bl=Convert.ToDouble(image.Width)/Convert.ToDouble(tWidth);

}

}

Bitmapb=newBitmap(image,Convert.ToInt32(image.Width/bl),Convert.ToInt32(image.Height/bl));

b.Save(newpath);

b.Dispose();

image.Dispose();

}

catch{}

}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。