Previous Article: This blog has not been updated for a long time. The main reason is that I am not taking the technical route now. I have passed the research technology age. Now we are doing a lot of scattered things.
Today, I suddenly received an email from the headhunter, and I was excited. Haha. Of course, let's look at the current situation. After all, the goals and requirements of life have changed. One time later
But TMD is excited...
I flipped through my folder, and there was really nothing original recently. I shared the image watermarking program I made a long time ago and made some small packages. Not completely original.
Imports Microsoft. VisualBasic
Imports System
Imports System. Drawing
Imports System. Drawing. Drawing2D
Imports System. Drawing. Imaging
Imports System. IO
'Code ex:
'Dim wm As New ImageDrawing. ImageModify
'Thefile. PostedFile. SaveAs (System. Web. HttpContext. Current. Server. MapPath ("1.jpg") // thisfile is a file upload html control
'Dim imgoutput As Image = System. Drawing. Bitmap. FromFile (System. Web. HttpContext. Current. Server. MapPath ("1.jpg "))
'With wm
'. WaterMarkImagePath = System. Web. HttpContext. Current. Server. MapPath ("a.gif ")
'. Your cypercent = 10
'. ModifyImagePath = System. Web. HttpContext. Current. Server. MapPath ("1.jpg ")
'. WaterImagePosition = ImageDrawing. ImageModify. WaterImagePositionType. RightBottom
'. OutPath = System. Web. HttpContext. Current. Server. MapPath ("2.jpg ")
'. DrawImage ()
'End
Namespace ImageDrawing
'// <Summary>
'// Image modification class, mainly used to protect the copyright of the image
'// </Summary>
Public Class ImageModify
# Region "member fields"
Private mModifyImagePath As String = ""
Private mWaterMarkImagePath As String = ""
Private mWaterImagePosition As WaterImagePositionType
Private mRightSpace As Int32
Private mBottoamSpace As Int32
Private m1_cypercent As Int32 = 50
Private mOutPath As String = ""
# End Region
'// Set the watermark position in the original image
Public Enum WaterImagePositionType
LeftTop
RightTop
Center
LeftBottom
RightBottom
Value
End Enum
# Region "propertys"
'// <Summary>
'// Obtain or set the image path to be modified
'// </Summary>
Public Property ModifyImagePath () As String
Get
Return mModifyImagePath
End Get
Set (ByVal Value As String)
MModifyImagePath = Value
End Set
End Property
'// <Summary>
'// Obtain or set the path of the image (watermark image)
'// </Summary>
Public Property WaterMarkImagePath () As String
Get
Return mWaterMarkImagePath
End Get
Set (ByVal Value As String)
MWaterMarkImagePath = Value
End Set
End Property
'// Set the watermark position in the original image
Public Property WaterImagePosition () As WaterImagePositionType
Get
Return mWaterImagePosition
End Get
Set (ByVal Value As WaterImagePositionType)
MWaterImagePosition = Value
End Set
End Property
'// <Summary>
'// Obtain or set the right margin of the watermark in the Modified Image
'// </Summary>
Public Property RightSpace () As Int32
Get
Return mRightSpace
End Get
Set (ByVal Value As Int32)
MRightSpace = Value
End Set
End Property
'// Obtain or set the watermark height from the bottom of the Modified Image
Public Property BottoamSpace () As Int32
Get
Return mBottoamSpace
End Get
Set (ByVal Value As Int32)
MBottoamSpace = Value
End Set
End Property
'// <Summary>
'// Obtain or set the watermark transparency. Note that the original image transparency percentage
'// </Summary>
Public Property specified cypercent () As Int32
Get
Return m1_cypercent
End Get
Set (ByVal Value As Int32)
If Value> = 0 And Value <= 100 Then
M1_cypercent = Value
Else
M1_cypercent = 50
End If
End Set
End Property
'// <Summary>
'// Obtain or set the path of the image to be output
'// </Summary>
Public Property OutPath () As String
Get
Return mOutPath
End Get
Set (ByVal Value As String)
MOutPath = Value
End Set
End Property
# End Region
# Region "methods"
'// <Summary>
'// Start to draw the watermark/image watermark, and set the transparency.
'// </Summary>
Public Function DrawImage () As Boolean
Dim bolReturn As Boolean = True
Dim modifyImage As Image = Nothing
Dim drawedImage As Image = Nothing
Dim g As Graphics = Nothing
Try
'// Create a graphic object
ModifyImage = Image. FromFile (Me. ModifyImagePath)
DrawedImage = Image. FromFile (Me. WaterMarkImagePath)
G = Graphics. FromImage (modifyImage)
'// Obtain the coordinates of the image to be drawn
Dim x As Int32
Dim y As Int32
Select Case Me. WaterImagePosition
Case WaterImagePositionType. Center
X = (modifyImage. Width-drawedImage. Width)/2
Y = (modifyImage. Height-drawedImage. Height)/2
Case WaterImagePositionType. LeftBottom
X = 0
Y = modifyImage. Height-drawedImage. Height
Case WaterImagePositionType. LeftTop
X = 0
Y = 0
Case WaterImagePositionType. RightBottom
X = modifyImage. Width-drawedImage. Width
Y = modifyImage. Height-drawedImage. Height
Case WaterImagePositionType. RightTop
X = modifyImage. Width-drawedImage. Width
Y = 0
Case WaterImagePositionType. Value
X = modifyImage. Width-Me. RightSpace
Y = modifyImage. Height-Me. BottoamSpace
Case Else
X = 0
Y = 0
End Select
If x <0 Then
X = 0
End If
If y <0 Then
Y = 0
End If
'// Set the color matrix
Dim colorMatrix As ColorMatrix = New ColorMatrix (New Single ()()_
{New Single () {1, 0, 0, 0, 0 },_
New Single () {0, 1, 0, 0 },_
New Single () {0, 0, 1, 0, 0 },_
New Single () {0, 0, 0, Convert. ToSingle (Me. Sort cypercent/100366f), 0 },_
New Single () {0, 0, 0, 0, 1 }})
Dim imgAttr As ImageAttributes = New ImageAttributes
ImgAttr. SetColorMatrix (colorMatrix, ColorMatrixFlag. Default, ColorAdjustType. Bitmap)
'// Draw a shadow Image
G. DrawImage (drawedImage, New Rectangle (x, y, drawedImage. Width, drawedImage. Height), 0, 0, drawedImage. Width, drawedImage. Height, GraphicsUnit. Pixel, imgAttr)
'// Save the file
Dim allowImageType As String () = {". jpg ",". gif ",". png ",". bmp ",". tiff ",". wmf ",". ico "}
Dim file As FileInfo = New FileInfo (Me. ModifyImagePath)
Dim imageType As ImageFormat = ImageFormat. Gif
Select Case file. Extension. ToLower ()
Case ". jpg"
ImageType = ImageFormat. Jpeg
Case ". gif"
ImageType = ImageFormat. Gif
Case ". png"
ImageType = ImageFormat. Png
Case ". bmp"
ImageType = ImageFormat. Bmp
Case ". tif"
ImageType = ImageFormat. Tiff
Case ". wmf"
ImageType = ImageFormat. Wmf
Case ". ico"
ImageType = ImageFormat. Icon
Case Else
ImageType = ImageFormat. Jpeg
End Select
Dim MS As MemoryStream = New MemoryStream ()
ModifyImage. Save (MS, imageType)
Dim imgData As Byte () = ms. ToArray ()
Dim fs As FileStream = Nothing
If Not Me. OutPath. Equals (String. Empty) Then
Fs = New FileStream (Me. OutPath, FileMode. Create, FileAccess. Write)
Else
Fs = New FileStream (Me. ModifyImagePath, FileMode. Create, FileAccess. Write)
End If
If Not fs Is Nothing Then
Fs. Write (imgData, 0, imgData. Length)
Fs. Close ()
End If
Catch ex As Exception
BolReturn = False
Finally
End Try
Return bolReturn
End Function
'Modify the image size
Public Function SizeChange (ByVal intWidth As Int32, ByVal intHeight As Int32) As Boolean
Dim bolReturn As Boolean = True
Try
Dim modifyImage As Image = Image. FromFile (Me. ModifyImagePath)
'//
Dim imageOut As Image = modifyImage. GetThumbnailImage (intWidth, intHeight, Nothing, System. IntPtr. Zero)
'// Save the file
Dim allowImageType As String () = {". jpg ",". gif ",". png ",". bmp ",". tiff ",". wmf ",". ico "}
Dim file As FileInfo = New FileInfo (Me. ModifyImagePath)
Dim imageType As ImageFormat
Select Case file. Extension. ToLower ()
Case ". jpg"
ImageType = ImageFormat. Jpeg
Case ". gif"
ImageType = ImageFormat. Gif
Case ". png"
ImageType = ImageFormat. Png
Case ". bmp"
ImageType = ImageFormat. Bmp
Case ". tif"
ImageType = ImageFormat. Tiff
Case ". wmf"
ImageType = ImageFormat. Wmf
Case ". ico"
ImageType = ImageFormat. Icon
Case Else
ImageType = ImageFormat. Jpeg
End Select
ImageOut. Save (OutPath, imageType)
Catch ex As Exception
BolReturn = False
Finally
End Try
Return bolReturn
End Function
# End Region
# Region "new"
Public Sub New ()
ModifyImagePath = ""
WaterMarkImagePath = ""
RightSpace = 0
BottoamSpace = 0
Repeated cypercent = 50
OutPath = ""
MWaterImagePosition = WaterImagePositionType. LeftTop
End Sub
# End Region
End Class
End Namespace
Call part:
Imports System. IO
Imports System. Drawing
Partial Class PicTest2
Inherits System. Web. UI. Page
Protected Sub button#click (ByVal sender As Object, ByVal e As System. EventArgs) Handles Button1.Click
'// Thefile is a file field html control.
Thefile. PostedFile. SaveAs (System. Web. HttpContext. Current. Server. MapPath ("BBS \ SnkProductImages \ 1.jpg "))
Dim memstream As System. IO. MemoryStream = New System. IO. MemoryStream ()
Dim imgoutput As System. Drawing. Image = System. Drawing. Bitmap. FromFile (System. Web. HttpContext. Current. Server. MapPath ("BBS \ SnkProductImages \ 1.jpg "))
'// Change to 80x80
Dim imgoutput2 As System. Drawing. Image = imgoutput. GetThumbnailImage (80, 80, Nothing, System. IntPtr. Zero)
Imgoutput2.Save (System. Web. HttpContext. Current. Server. MapPath ("BBS \ SnkProductImages \ 2.jpg"), System. Drawing. Imaging. ImageFormat. Jpeg)
Response. Write (thefile. PostedFile. FileName)
Response. Write ("len:" + memstream. length. tostring ())
Imgoutput. Dispose ()
Imgoutput2.Dispose ()
Response. Write ("Upload successful! ")
Response. Write (System. Web. HttpContext. Current. Server. MapPath ("BBS \ SnkProductImages \ 2.jpg "))
'// Add a text watermark. Note that the code here cannot coexist with the image watermark code below.
Dim g As Graphics = Graphics. FromImage (imgoutput)
G. DrawImage (imgoutput, 0, 0, imgoutput. Width, imgoutput. Height)
Dim f As Font = New Font ("Verdana", 32)
Dim B As Brush = New SolidBrush (Color. Red)
Dim addText As String = "SNK Star"
G. DrawString (addText, f, B, 10, 10)
G. Dispose ()
Imgoutput. Save (System. Web. HttpContext. Current. Server. MapPath ("BBS \ SnkProductImages \ 3.jpg"), System. Drawing. Imaging. ImageFormat. Jpeg)
End Sub
End Class