多语言展示
当前在线:1627今日阅读:23今日分享:25

用asp.net - vb.net创建图片水印

网站上的图片水印,列出asp.net(vb.net)的代码,代码用的是VB。看下最终效果图:
工具/原料

visual studio.net, asp.net, vb.net

步骤/方法
2

当主函数被实例化,定义了两个string类型变量。第一个将定义哪里去找照片,水印和输出新图片(的位置)。第二个将定义要用来作为我们的水印的一部分的版权字符串。 Dim WorkingDirectory As String = 'D:\ImagePath' ' 指定路径 Dim Copyright As String = 'www.ninone.com' ' 指定要添加在图片上的文字

3

从一个指定文件创建了一个Image 对象,然后为它的Width 和Height定义变量。这些长度待会被用来建立一个以24 bits 每像素的格式作为颜色数据的Bitmap对象。 Dim imgPhoto As Image = Image.FromFile(WorkingDirectory '\W11.jpg') ' 指定原始图片文件 Dim phWidth As Integer = imgPhoto.Width Dim phHeight As Integer = imgPhoto.Height Dim bmPhoto As Bitmap = New Bitmap(phWidth, phHeight, PixelFormat.Format24bppRgb) bmPhoto.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution) Dim grPhoto As Graphics = Graphics.FromImage(bmPhoto)

4

载入水印图片,水印图片已经被保存为一个BMP文件,以绿色(A=0,R=0,G=255,B=0)作为背景颜色。再一次,会为它的Width 和Height定义一个变量  Dim imgWatermark As Image = New Bitmap(WorkingDirectory '\WM.bmp') '指定要在原始图片上新加图标的文件 Dim wmWidth As Integer = imgWatermark.Width Dim wmHeight As Integer = imgWatermark.Height ' 这个代码以100%它的原始大小绘制imgPhoto到Graphics对象的(x=0,y=0)位置。以后所有的绘图都将发生在原来照片的顶部。 grPhoto.SmoothingMode = SmoothingMode.AntiAlias grPhoto.DrawImage(imgPhoto, New Rectangle(0, 0, phWidth, phHeight), 0, 0, phWidth, phHeight, GraphicsUnit.Pixel) ' 为了最大化版权信息的大小,我们将测试7种不同的字体大小来决定我们能为我们的照片宽度使用的可能的最大大小。 ' 为了有效地完成这个,我们将定义一个整型数组,接着遍历这些整型值测量不同大小的版权字符串。一旦我们决定了可能的最大大小,我们就退出循环,绘制文本。 Dim sizes As Integer() = New Integer() {32, 28, 24, 20, 16, 14, 12} Dim crFont As Font = Nothing Dim crSize As SizeF = New SizeF Dim i As Integer For i = 0 To 6 crFont = New Font('arial', sizes(i), FontStyle.Bold) crSize = grPhoto.MeasureString(Copyright, crFont) If Int(crSize.Width) < Int(phWidth) Then Exit For End If Next ' 因为所有的照片都有各种各样的高度,所以就决定了从图象底部开始的20%的位置开始。 ' 使用Copyright字符串的高度来决定绘制字符串合适的Y坐标轴。通过计算图像的中心来决定X轴,然后定义一个StringFormat对象,设置StringAlignment为Center。 Dim yPixlesFromBottom As Integer = Int(phHeight * 0.2) Dim yPosFromBottom As Double = ((phHeight - yPixlesFromBottom) - (crSize.Height / 2)) Dim xCenterOfImg As Double = (phWidth / 2) Dim StrFormat As StringFormat = New StringFormat StrFormat.Alignment = StringAlignment.Center ' 现在我们已经有了所有所需的位置坐标来使用60%黑色的一个Color(alpha值153)创建一个SolidBrush。在偏离右边1像素,底部1像素的合适位置绘制版权字符串。

5

这段偏离将用来创建阴影效果。使用Brush重复这样一个过程,在前一个绘制的文本顶部绘制同样的文本。 Dim semiTransBrush2 As SolidBrush = New SolidBrush(Color.FromArgb(153, 0, 0, 0)) grPhoto.DrawString(Copyright, crFont, semiTransBrush2, New PointF(xCenterOfImg 1, yPosFromBottom 1), StrFormat) Dim semiTransBrush As SolidBrush = New SolidBrush(Color.FromArgb(153, 255, 255, 255)) grPhoto.DrawString(Copyright, crFont, semiTransBrush, New PointF(xCenterOfImg, yPosFromBottom), StrFormat) '根据前面修改后的照片创建一个Bitmap。把这个Bitmap载入到一个新的Graphic对象。 Dim bmWatermark As Bitmap = New Bitmap(bmPhoto) bmWatermark.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution) Dim grWatermark As Graphics = Graphics.FromImage(bmWatermark) Dim imageAttributes As ImageAttributes = New ImageAttributes Dim colorMap As ColorMap = New ColorMap ' 通过定义一个ImageAttributes对象并设置它的两个属性,我们就是实现了两个颜色的处理,以达到半透明的水印效果。 ' 处理水印图象的第一步是把背景图案变为透明的(Alpha=0, R=0, G=0, B=0)。我们使用一个Colormap和定义一个RemapTable来做这个。 ' 就像前面展示的,我的水印被定义为100%绿色背景,我们将搜到这个颜色,然后取代为透明。 colorMap.OldColor = Color.FromArgb(255, 0, 255, 0) colorMap.NewColor = Color.FromArgb(0, 0, 0, 0) Dim remapTable As ColorMap() = {colorMap} imageAttributes.SetRemapTable(remapTable, ColorAdjustType.Bitmap) ' 第二个颜色处理用来改变水印的不透明性。通过应用包含提供了坐标的RGBA空间的5x5矩阵来做这个。 ' 通过设定第三行、第三列为0.5f我们就达到了一个不透明的水平。 ' 结果是水印会轻微地显示在图象底下一些。 Dim colorMatrixElements()() As Single = {New Single() {1.0F, 0.0F, 0.0F, 0.0F, 0.0F}, New Single() {0.0F, 1.0F, 0.0F, 0.0F, 0.0F}, New Single() {0.0F, 0.0F, 1.0F, 0.0F, 0.0F}, New Single() {0.0F, 0.0F, 0.0F, 0.5F, 0.0F}, New Single() {0.0F, 0.0F, 0.0F, 0.0F, 1.0F}} Dim wmColorMatrix As ColorMatrix = New ColorMatrix(colorMatrixElements) imageAttributes.SetColorMatrix(wmColorMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap) ' 随着两个颜色处理加入到imageAttributes对象,我们现在就能在照片右手边上绘制水印了。 ' 我们会偏离10像素到底部,10像素到左边。 Dim xPosOfWm As Integer = ((phWidth - wmWidth) - 10) Dim yPosOfWm As Integer = 10 grWatermark.DrawImage(imgWatermark, New Rectangle(xPosOfWm, yPosOfWm, wmWidth, wmHeight), 0, 0, wmWidth, wmHeight, GraphicsUnit.Pixel, imageAttributes)

6

最后的步骤将是使用新的Bitmap取代原来的Image。销毁两个Graphic对象,然后把Image保存到文件系统。 imgPhoto = bmWatermark grPhoto.Dispose() grWatermark.Dispose() imgPhoto.Save(WorkingDirectory '\W11_final.jpg', ImageFormat.Jpeg) '指定要在原始图片上新加图标的文件 imgPhoto.Dispose() imgWatermark.Dispose()

注意事项

以上代码已经过测试,直接在VS.NET里拷贝粘贴即可运行。

推荐信息