上傳圖片畫帶陰影的浮水印.(C#)
來源:互聯網
上載者:User
ASP.NET上傳圖片後,要加上浮水印.比如網址. 文字為白色.但是當背景是白色的時候,浮水印根本看不到. 到處搜尋資料,找畫文字陰影的辦法. 找了好幾個,最後找了個老外的文章,和.Net內建文檔裡的說法一樣. 居然是先畫2-5個alpha值不同的灰色文字當陰影.. 再在上面,畫正常顏色的文本....汗!!!!!!!!!!!!!!!!!!111 沒有辦法了,只好將就用了.效果還一般. Font font=new Font("Arial Black",15,FontStyle.Bold); SizeF sf=g.MeasureString("ImgGood.Com",font); PointF pf=new PointF(); pf.X=(250-sf.Width)/2; pf.Y=(bHeight-sf.Height)/2; //建立浮水印bmp Bitmap floatBmp=new Bitmap((int)sf.Width+3,(int)sf.Height+3,System.Drawing.Imaging.PixelFormat.Format32bppArgb); //Bitmap floatBmp=new Bitmap(250,100); //Bitmap floatBmp=new Bitmap((int)sf.Width,(int)sf.Height); Graphics fg=Graphics.FromImage(floatBmp); //畫上陰影字元 PointF pt=new PointF(0,0); System.Drawing.Brush TransparentBrush0 = new System.Drawing.SolidBrush( System.Drawing.Color.FromArgb(50,System.Drawing.Color.Black ) ) ; System.Drawing.Brush TransparentBrush1 = new System.Drawing.SolidBrush( System.Drawing.Color.FromArgb(20,System.Drawing.Color.Black ) ) ; fg.DrawString("ImgGood.Com",font,TransparentBrush0,pt.X,pt.Y+1); fg.DrawString("ImgGood.Com",font,TransparentBrush0,pt.X+1,pt.Y); fg.DrawString("ImgGood.Com",font,TransparentBrush1,pt.X+1,pt.Y+1); fg.DrawString("ImgGood.Com",font,TransparentBrush1,pt.X,pt.Y+2); fg.DrawString("ImgGood.Com",font,TransparentBrush1,pt.X+2,pt.Y); TransparentBrush0.Dispose(); TransparentBrush1.Dispose(); //畫上LOGO字元 fg.SmoothingMode=System.Drawing.Drawing2D.SmoothingMode.HighQuality; //fg.Clear(Color.Black); fg.DrawString("ImgGood.Com",font,new SolidBrush(Color.White),pt.X,pt.Y,StringFormat.GenericDefault); //畫浮水印到 大圖 fg.Save(); fg.Dispose(); //設定透明映像的顏色屬性 float[][] ptsArray ={ 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, 0.5f, 0}, new float[] {0, 0, 0, 0, 1}}; ColorMatrix clrMatrix = new ColorMatrix(ptsArray); ImageAttributes imgAttributes = new ImageAttributes(); imgAttributes.SetColorMatrix(clrMatrix, ColorMatrixFlag.Default,ColorAdjustType.Bitmap); g.DrawImage(floatBmp,new Rectangle(10,10,(int)sf.Width,(int)sf.Height),0,0,(int)sf.Width,(int)sf.Height,GraphicsUnit.Pixel,imgAttributes); //======================================================================== g.Save(); g.Dispose(); bigBmp.Save(filePath + "B/" + fileName); bigBmp.Dispose(); 代碼不難,不解決了