想把图片用VB.NET加个水印,并且水印可以移动。请问各位老师这个怎么弄。
------解决思路----------------------
Public Class Form1
Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
...
#End Region
Private watermark_bm As Bitmap
Private source_bm As Bitmap
Private rcWatermark As Rectangle
Private ptLast As Point
Private isDraging As Boolean = False
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
watermark_bm = New Bitmap(picWatermark2.Image)
source_bm = New Bitmap(System.IO.Path.Combine(Application.StartupPath, "..\dog.jpg"))
rcWatermark = New Rectangle((source_bm.Width - source_bm.Width) \ 2, _
2 * (source_bm.Height - source_bm.Height) \ 3, _
watermark_bm.Width, _
watermark_bm.Height)
RedrawImage()
End Sub
Private Sub picImage_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles picImage.MouseDown
If e.Button = MouseButtons.Left Then
If rcWatermark.Contains(e.X, e.Y) Then
ptLast = New Point(e.X, e.Y)
isDraging = True
End If
End If
End Sub
Private Sub picImage_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles picImage.MouseMove
If isDraging Then
rcWatermark.Offset(e.X - ptLast.X, e.Y - ptLast.Y)
ptLast = New Point(e.X, e.Y)
RedrawImage()
End If
End Sub
Private Sub picImage_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles picImage.MouseUp
isDraging = False
End Sub
Private Sub RedrawImage()
Dim result_bm As New Bitmap(picImage.Image)
DrawBackground(source_bm, result_bm)
DrawBorder(result_bm)
DrawWatermark(watermark_bm, result_bm, rcWatermark.X, rcWatermark.Y)
picImage.Image = result_bm
End Sub
Private Sub DrawBackground(ByVal back_bm As Bitmap, ByVal result_bm As Bitmap)
Dim gr As Graphics = Graphics.FromImage(result_bm)
gr.DrawImage(back_bm, 0, 0)
End Sub
Private Sub DrawBorder(ByVal result_bm As Bitmap)
Dim gr As Graphics = Graphics.FromImage(result_bm)
Dim greenPen As New Pen(Color.Green)
gr.DrawRectangle(greenPen, rcWatermark)
End Sub
' Copy the watermark image over the result image.
Private Sub DrawWatermark(ByVal watermark_bm As Bitmap, ByVal result_bm As Bitmap, ByVal x As Integer, ByVal y As Integer)
Const ALPHA As Byte = 128
' Set the watermark's pixels' Alpha components.
Dim clr As Color
For py As Integer = 0 To watermark_bm.Height - 1
For px As Integer = 0 To watermark_bm.Width - 1
clr = watermark_bm.GetPixel(px, py)
watermark_bm.SetPixel(px, py, Color.FromArgb(ALPHA, clr.R, clr.G, clr.B))
Next px
Next py
' Set the watermark's transparent color.
watermark_bm.MakeTransparent(watermark_bm.GetPixel(0, 0))
' Copy onto the result image.
Dim gr As Graphics = Graphics.FromImage(result_bm)
gr.DrawImage(watermark_bm, x, y)
End Sub
End Class