' ประกาศดัวแปร XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Dim chkUpload As Boolean
Dim picName, filepath As String
Dim newID, quantity, wi, hi As Integer
Dim bm As Image
' XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX ประกาศดัวแปร
checkPicture()
Sub checkPicture()
Dim chkuploadedFiles As HttpFileCollection = Request.Files
Dim chkPostedFile As HttpPostedFile = chkuploadedFiles(0)
If (chkPostedFile.ContentType = "image/pjpeg") Then
picName = Format(CInt(newID), "0000000") & "_00"
lblPicture.Text = ""
chkUpload = True
Else
picName = "0000000_00"
lblPicture.Text = "เฉพาะนามสกุล .jpg เท่านั้น"
chkUpload = False
End If
End Sub
สร้างรหัสใหม่
Sub funcNewID()
com = New SqlCommand("รหัสล่าสุด", Conn)
Conn.Open() : data = com.ExecuteScalar() : Conn.Close()
If data = "" Then
newID = 1
Else
newID = CInt(data) + 1
End If
End Sub
เมื่อคลิก
Protected Sub btnAdd_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAdd.Click
funcNewID()
checkPicture()
If chkUpload = True Then
' ส่วนนี้ใช้จัดเก็บชื่อลงฐานข้อมูล (ออกแบบเอง)
' picName & ".jpg" <<< ชื่อที่เก็บลง DB
funcResize()
Response.Redirect("Url ที่ต้องการไปต่อ")
End If
End Sub
สร้าง Thumb picture (แบบยังไม่มีลายน้ำ)
Sub funcResize()
filepath = Server.MapPath("imgProduct")
Dim uploadedFiles As HttpFileCollection = Request.Files
Dim userPostedFile As HttpPostedFile = uploadedFiles(0)
userPostedFile.SaveAs(filepath & "\" & picName & "_ori.jpg")
bm = Image.FromFile(filepath & "\" & picName & "_ori.jpg")
Dim x As Int32 ' variable for new width size
Dim y As Int32 ' variable for new height size
Dim width As Integer ' Val(x) 'image width.
Dim height As Integer ' Val(y) 'image height
Dim thumb As Bitmap
Dim g As Graphics
wi = bm.Width
hi = bm.Height
Dim newWidth As Double = 0
Dim newHeight As Double = 0
Dim persent As Double = 0 ' ค่า % ของรูป
Dim different As Double = 0 ' ส่วนต่างที่ทำให้ถึง 150 px
If CInt(wi) > 150 Then 'ถ้าภาพใหญ่กว่า 150 px จะย่อเหลือความกว้าง 150 โดยคงอัตราส่วนภาพ ไม่ให้บิดเบี้ยว (อิงจากค่า %)
persent = (150 / wi) * 100
newWidth = (wi * persent) / 100
newHeight = (hi * persent) / 100
different = 150 - CInt(newWidth)
x = CInt(newWidth) + different
y = CInt(newHeight) + different
' ขั้นตอนนี้ อ่านเองนะครับ ซับซ้อน ไม่อยากอธิบาย ใช้เวลา ราวๆ 4 ชั่วโมง เพื่อหาสูตรนี้
Else ' ถ้าไม่ถึง 150 ก็จัดบันทึกขนาด ก x ส เลย
x = bm.Width
y = bm.Height
End If
width = x
height = y
thumb = New Bitmap(width, height)
g = Graphics.FromImage(thumb)
g.InterpolationMode = Drawing2D.InterpolationMode.HighQualityBicubic
g.DrawImage(bm, New Rectangle(0, 0, width, height), New Rectangle(0, 0, bm.Width, bm.Height), GraphicsUnit.Pixel)
g.Dispose()
thumb.Save(filepath & "\" & picName & "_thumb.jpg", ImageFormat.Jpeg) 'can use any image format
bm.Dispose()
thumb.Dispose()
g.Dispose()
picWaterMark()
End Sub
สร้าง Water mark
Sub picWaterMark()
Dim webname As String
webname = ClassMarket.Web_Name.ToUpper & ".SIAM-MINIMART.COM"
data = webname & " " & webname & " " & webname
data &= " " & webname & " " & webname
Dim objFont As New Font("MS Sans Serif", 12) ', FontStyle.Strikeout
Dim MSG As String = data
Dim objBrush As New HatchBrush(HatchStyle.BackwardDiagonal, Color.White, Color.White) 'HatchStyle.DiagonalCross, Color.Red, Color.Blue
Dim PointX, PointY As Integer
PointX = hi
PointY = hi - 16
Dim b As New Bitmap(wi, hi, PixelFormat.Format32bppPArgb)
Dim g As Graphics = Graphics.FromImage(b)
Dim b2 As New Bitmap(filepath & "\" & picName & "_ori.jpg")
Dim br As New TextureBrush(b2)
g.FillRectangle(br, New Rectangle(0, 0, wi, hi))
g.RotateTransform(45)
Dim l As Integer = 1
Dim Massage As String
For i As Integer = 0 To hi Step 100
If (l Mod 2) = 1 Then
Massage = data
Else
Massage = " " & data
End If
g.DrawString(Massage, objFont, objBrush, i, i - 16)
l += 1
Next
l = 1
For i As Integer = 100 To wi Step 100
If (l Mod 2) = 0 Then
Massage = data
Else
Massage = " " & data
End If
g.DrawString(Massage, objFont, objBrush, i, 0 - (i + 16))
l += 1
Next
'Dim x, y As Integer
'For x = -1000 To 2000 Step 100
' For y = -1010 To 2000 Step 20
' g.DrawString(x & "x" & y, objFont, objBrush, x, y)
' Next
'Next
'Response.ContentType = "image/gif"
b.Save(filepath & "\" & picName & ".jpg", ImageFormat.Jpeg)
b.Dispose()
b2.Dispose()
g.Dispose()
br.Dispose()
objBrush.Dispose()
Dim file As New FileInfo(filepath & "\" & picName & "_ori.jpg")
file.Delete()
' ถ้าไม่อยากลบรูปต้นฉบับ ก็ Comment หรือ ลบ 2 บรรทัดบนนี้ออก
End Sub
Dim b As New Bitmap(wi, hi, PixelFormat.Format32bppPArgb)
Dim g As Graphics = Graphics.FromImage(b)
Dim b2 As New Bitmap(filepath & "\" & picName & "_ori.jpg")
Dim br As New TextureBrush(b2)
g.FillRectangle(br, New Rectangle(0, 0, wi, hi))
g.RotateTransform(45)
Dim l As Integer = 1
Dim Massage As String
For i As Integer = 0 To hi Step 100
If (l Mod 2) = 1 Then
Massage = data
Else
Massage = " " & data
End If
g.DrawString(Massage, objFont, objBrush, i, i - 16)
l += 1
Next
l = 1
For i As Integer = 100 To wi Step 100
If (l Mod 2) = 0 Then
Massage = data
Else
Massage = " " & data
End If
g.DrawString(Massage, objFont, objBrush, i, 0 - (i + 16))
l += 1
Next