Thursday, June 26, 2008

Image save in SQL server ( vb.net)

'This code is generate Binary data from path of image..
'Programmer can Save image into PC
'And this code can decrease resolution of image ( for Logo)
Private Sub btnBrowse_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBrowse.Click

OpenFileDialog1.ShowDialog()
dim path as string = OpenFileDialog1.FileName()
If path = "OpenFileDialog1" Then
MsgBox("Select Image As Division Logo")
End If
Dim ext As String = System.IO.Path.GetExtension(path)
retunSTR = Division.ImageLoad(ext, path)
If retunSTR = "f" Then
MsgBox("Please, Select *.jpg , *.jpeg , *bmp , *.png ,*.gif files ")
Else
DivisionLogo1.Load(retunSTR)
End If
End Sub


Public Function ImageLoad(ByVal ext As String, ByVal path As String) As String
Dim ExtFalse As String = ""
If (ext = ".jpg" Or ext = ".jpeg" Or ext = ".bmp" Or ext = ".png" Or ext = ".gif") Then
If path <> "OpenFileDialog1" Then
Dim Imgwidth As Integer = 0
Dim imgHeight As Integer = 0
Dim curImg As System.Drawing.Image = System.Drawing.Image.FromFile(path)
Imgwidth = curImg.Width
imgHeight = curImg.Height
Dim fnlW As Integer = 100 'Image size and extension validation
Dim fnlH As Integer = 100
Dim tumW As Integer = 100
Dim tumH As Integer = 100
'Dim curFile As String
Dim oldW, oldH, newW, newH, defW, defH, alow, aloh As Integer
Dim kla, klb As Double
'Dim curImg As System.Drawing.Image = System.Drawing.Image.FromFile(curFile)
oldW = Imgwidth
oldH = imgHeight
If oldW > fnlW And oldH > fnlH Then
Dim thumb As System.Drawing.Image
Dim ext1 = curImg.RawFormat
Dim inp As New IntPtr()
kla = oldW / fnlW
klb = oldH / kla
If klb <= fnlH Then
kla = oldH / fnlH
End If
newW = oldW / kla
newH = oldH / kla
Dim imgRszd As New Bitmap(curImg, newW, newH)
curImg.Dispose()
defW = newW - fnlW
defH = newH - fnlH
If defW < 0 Then defW = 0
If defH < 0 Then defH = 0
Dim imgCrpd As New Bitmap(fnlW, fnlH)
Dim myGraphic = Graphics.FromImage(imgCrpd)
alow = defW / 2
aloh = defH / 2
myGraphic.DrawImage(imgRszd, New Rectangle(0, 0, fnlW, fnlH), alow, aloh, fnlW, fnlH, GraphicsUnit.Pixel)
imgCrpd.Save(System.IO.Path.GetFileName(path), ext1)
thumb = imgCrpd.GetThumbnailImage(tumW, tumH, Nothing, inp)
thumb.Save("th_" & System.IO.Path.GetFileName(path), ext1)
Dim path_oldfilenm As String = System.IO.Path.GetFileName(path)
Dim path_newfilenm As String = "th_" & path_oldfilenm
Dim path2 As New StringBuilder(path)
path2.Replace(path_oldfilenm, path_newfilenm)
path = path2.ToString()
imgRszd.Dispose()
myGraphic.dispose()
imgCrpd.Dispose()
curImg.Dispose()
Return path
Else
curImg.Dispose()
Return path
End If
End If
Else
ExtFalse = "f"
Return ExtFalse ' Select extension
End If
Return 1
End Function

No comments: