After looking at about 200 examples on this and other sides, I combined several scripts that I found and created one (read I did frankenstein script) to upload an image, give it a random name, verify that a file with that same names doesn't already exists
on the server, resize the image 3 different ways; one keeping aspect ratio and two square thumbnails; and save them to different folders in the server. This is the code:
Imports System.Data.SqlClient
Imports System.Data
Imports System.Drawing
Imports System.Drawing.Imaging
Imports System.Drawing.Drawing2D
Imports System.Configuration
Imports System.Web
Imports System.Web.Security
Imports System.IO
Partial Public Class uploadtest_upload1
Inherits System.Web.UI.Page
Protected Sub btnUpload_Click(ByVal sender As Object, ByVal e As EventArgs)
Dim aspectRatio As Single
Dim NewImageName As String = System.Guid.NewGuid().ToString()
NewImageName = NewImageName.Replace("-", String.Empty)
NewImageName = NewImageName.Substring(0, 15)
Dim ULdir_Regular As String = "/images/regular/"
Dim Large_Height As Integer = 600
Dim Large_Width As Integer = 400
Dim ULdir_Medium As String = "/images/medium/"
Dim Medium_Size As Integer = 200
Dim ULdir_Small As String = "/images/small/"
Dim Small_Size As Integer = 60
Dim myPathRegular As String = ULdir_Regular & NewImageName & ".jpg"
Dim myPathMedium As String = ULdir_Medium & NewImageName & ".jpg"
Dim myPathSmall As String = ULdir_Small & NewImageName & ".jpg"
'Verify that image name doesn't already exist on sever. If does, assign a new Number. Can we get this to keep trying is other number also exists?
If File.Exists(myPathRegular) Or File.Exists(myPathMedium) Or File.Exists(myPathSmall) Then
NewImageName = System.Guid.NewGuid().ToString()
NewImageName = NewImageName.Replace("-", String.Empty)
NewImageName = NewImageName.Substring(0, 15)
End If
If PUpload.PostedFile IsNot Nothing AndAlso PUpload.PostedFile.FileName <> "" Then
Dim ImageExtension As String = System.IO.Path.GetExtension(PUpload.FileName)
If (ImageExtension.ToUpper() = ".JPG") Then
' Get Original Image Information
Dim Original_Image As System.Drawing.Image = System.Drawing.Image.FromStream(PUpload.PostedFile.InputStream)
Dim Original_Height As Integer = Original_Image.Height
Dim Original_Width As Integer = Original_Image.Width
'Calculate Aspect/Size: Large Version
If Original_Width > Original_Height Then
aspectRatio = CSng(Original_Height) / CSng(Original_Width)
Large_Height = CInt(Math.Truncate(Large_Width * aspectRatio))
Else
aspectRatio = CSng(Original_Width) / CSng(Original_Height)
Large_Width = CInt(Math.Truncate(Large_Height * aspectRatio))
End If
'Create Temp Image, Set Quality & Save Image to Disk: Large Version
Dim tempLarge As System.Drawing.Image = New Bitmap(Large_Width, Large_Height, Original_Image.PixelFormat)
Dim LargeGraphic As Graphics = Graphics.FromImage(tempLarge)
LargeGraphic.CompositingQuality = CompositingQuality.HighQuality
LargeGraphic.SmoothingMode = SmoothingMode.HighQuality
LargeGraphic.InterpolationMode = InterpolationMode.HighQualityBicubic
Dim LargeRectangle As New Rectangle(0, 0, Large_Width, Large_Height)
LargeGraphic.DrawImage(Original_Image, LargeRectangle)
tempLarge.Save(ULdir_Regular & NewImageName, ImageFormat.Jpeg)
'Calculate Aspect/Size: Medium Version
Dim tempMedium As System.Drawing.Image = New Bitmap(Medium_Size, Medium_Size)
Dim am = Math.Min(Original_Width, Original_Height)
Dim xm = (Original_Width - am) / 2
Dim ym = (Original_Height - am) / 2
'Create Temp Image, Set Quality & Save Image to Disk: Large Version
Dim MediumGraphic As Graphics = Graphics.FromImage(tempMedium)
MediumGraphic.CompositingQuality = CompositingQuality.HighQuality
MediumGraphic.SmoothingMode = SmoothingMode.HighQuality
MediumGraphic.InterpolationMode = InterpolationMode.HighQualityBicubic
Dim MediumRectangle = New Rectangle(0, 0, Medium_Size, Medium_Size)
MediumGraphic.DrawImage(Original_Image, MediumRectangle, xm, ym, am, am, GraphicsUnit.Pixel)
tempMedium.Save(ULdir_Medium & NewImageName, ImageFormat.Jpeg)
'Calculate Aspect/Size: Medium Version
Dim tempSmall As System.Drawing.Image = New Bitmap(Small_Size, Small_Size)
Dim a = Math.Min(Original_Width, Original_Height)
Dim x = (Original_Width - a) / 2
Dim y = (Original_Height - a) / 2
'Create Temp Image, Set Quality & Save Image to Disk: Large Version
Dim SmallGraphic As Graphics = Graphics.FromImage(tempSmall)
SmallGraphic.CompositingQuality = CompositingQuality.HighQuality
SmallGraphic.SmoothingMode = SmoothingMode.HighQuality
SmallGraphic.InterpolationMode = InterpolationMode.HighQualityBicubic
Dim SmallRectangle = New Rectangle(0, 0, Small_Size, Small_Size)
SmallGraphic.DrawImage(Original_Image, SmallRectangle, x, y, a, a, GraphicsUnit.Pixel)
tempSmall.Save(ULdir_Small & NewImageName, ImageFormat.Jpeg)
'Clean Up
tempLarge.Dispose()
LargeGraphic.Dispose()
tempMedium.Dispose()
MediumGraphic.Dispose()
tempSmall.Dispose()
SmallGraphic.Dispose()
Original_Image.Dispose()
Dim myID As Integer
If IsNumeric(Request.QueryString("i")) Then
myID = Request.QueryString("i")
myID = Convert.ToInt32(myID)
Else
myID = Convert.ToInt32("0")
End If
Dim connStr As String = ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString
Dim DBConn As New SqlConnection(connStr)
Dim DBCmd As New SqlCommand
Dim sql As String = "in_photo_SP"
Dim myImage As String = NewImageName & ".jpg"
DBConn.Open()
Try
DBCmd = New SqlCommand(sql, DBConn)
DBCmd.CommandType = CommandType.StoredProcedure
DBCmd.Parameters.Add("@TableID", SqlDbType.Int).Value = myID
DBCmd.Parameters.Add("@photo", SqlDbType.NVarChar).Value = myImage
DBCmd.Parameters.Add("@caption", SqlDbType.NVarChar).Value = caption.Text
DBCmd.ExecuteNonQuery()
Catch exp As Exception
Response.Write(exp)
End Try
DBCmd.Dispose()
DBConn.Close()
DBConn = Nothing
Else
lblMessage.Text = "Please only upload .jpg files"
lblMessage.Visible = True
End If
Else
lblMessage.Text = "No file selected"
lblMessage.Visible = True
End If
End Sub
End Class
I have two problems with the code: 1) I look in the server once for a file with same name and if one exist, i use a new name. But really, I should keep looking until instead of just using the new one, just in case the new already exists to. Anyway to loop this until it finds a name is not using? 2) I'm getting an error on line 95 and 112:
I get the error on intellisense: Overload resolution failed because no accessible DrawImage can be called without a narrowing conversion. ..... Argument matching parameter 'srcX' narrows from 'Double' to Inter... narrows from 'Double' to 'Single'.
You could replace it with it's overloaded method, you should go for .ToString("N"). Check out
here for more info.
As you're using GUID, which generates random 128bit integer, I think there're very little chances that you'll have filename that already exists on server so Don't worry about that.
diomedes
'Calculate Aspect/Size: Medium Version
Dim tempMedium As System.Drawing.Image = New Bitmap(Medium_Size, Medium_Size)
Dim am = Math.Min(Original_Width, Original_Height)
Dim xm = (Original_Width - am) / 2
Dim ym = (Original_Height - am) / 2
diomedes
Dim MediumRectangle = New Rectangle(0, 0, Medium_Size, Medium_Size)
Diomedes
Member
202 Points
145 Posts
Overload resolution failed because no accessible DrawImage can be called without a narrowing conv...
Jan 29, 2011 02:09 AM|LINK
Hi,
After looking at about 200 examples on this and other sides, I combined several scripts that I found and created one (read I did frankenstein script) to upload an image, give it a random name, verify that a file with that same names doesn't already exists on the server, resize the image 3 different ways; one keeping aspect ratio and two square thumbnails; and save them to different folders in the server. This is the code:
Imports System.Data.SqlClient Imports System.Data Imports System.Drawing Imports System.Drawing.Imaging Imports System.Drawing.Drawing2D Imports System.Configuration Imports System.Web Imports System.Web.Security Imports System.IO Partial Public Class uploadtest_upload1 Inherits System.Web.UI.Page Protected Sub btnUpload_Click(ByVal sender As Object, ByVal e As EventArgs) Dim aspectRatio As Single Dim NewImageName As String = System.Guid.NewGuid().ToString() NewImageName = NewImageName.Replace("-", String.Empty) NewImageName = NewImageName.Substring(0, 15) Dim ULdir_Regular As String = "/images/regular/" Dim Large_Height As Integer = 600 Dim Large_Width As Integer = 400 Dim ULdir_Medium As String = "/images/medium/" Dim Medium_Size As Integer = 200 Dim ULdir_Small As String = "/images/small/" Dim Small_Size As Integer = 60 Dim myPathRegular As String = ULdir_Regular & NewImageName & ".jpg" Dim myPathMedium As String = ULdir_Medium & NewImageName & ".jpg" Dim myPathSmall As String = ULdir_Small & NewImageName & ".jpg" 'Verify that image name doesn't already exist on sever. If does, assign a new Number. Can we get this to keep trying is other number also exists? If File.Exists(myPathRegular) Or File.Exists(myPathMedium) Or File.Exists(myPathSmall) Then NewImageName = System.Guid.NewGuid().ToString() NewImageName = NewImageName.Replace("-", String.Empty) NewImageName = NewImageName.Substring(0, 15) End If If PUpload.PostedFile IsNot Nothing AndAlso PUpload.PostedFile.FileName <> "" Then Dim ImageExtension As String = System.IO.Path.GetExtension(PUpload.FileName) If (ImageExtension.ToUpper() = ".JPG") Then ' Get Original Image Information Dim Original_Image As System.Drawing.Image = System.Drawing.Image.FromStream(PUpload.PostedFile.InputStream) Dim Original_Height As Integer = Original_Image.Height Dim Original_Width As Integer = Original_Image.Width 'Calculate Aspect/Size: Large Version If Original_Width > Original_Height Then aspectRatio = CSng(Original_Height) / CSng(Original_Width) Large_Height = CInt(Math.Truncate(Large_Width * aspectRatio)) Else aspectRatio = CSng(Original_Width) / CSng(Original_Height) Large_Width = CInt(Math.Truncate(Large_Height * aspectRatio)) End If 'Create Temp Image, Set Quality & Save Image to Disk: Large Version Dim tempLarge As System.Drawing.Image = New Bitmap(Large_Width, Large_Height, Original_Image.PixelFormat) Dim LargeGraphic As Graphics = Graphics.FromImage(tempLarge) LargeGraphic.CompositingQuality = CompositingQuality.HighQuality LargeGraphic.SmoothingMode = SmoothingMode.HighQuality LargeGraphic.InterpolationMode = InterpolationMode.HighQualityBicubic Dim LargeRectangle As New Rectangle(0, 0, Large_Width, Large_Height) LargeGraphic.DrawImage(Original_Image, LargeRectangle) tempLarge.Save(ULdir_Regular & NewImageName, ImageFormat.Jpeg) 'Calculate Aspect/Size: Medium Version Dim tempMedium As System.Drawing.Image = New Bitmap(Medium_Size, Medium_Size) Dim am = Math.Min(Original_Width, Original_Height) Dim xm = (Original_Width - am) / 2 Dim ym = (Original_Height - am) / 2 'Create Temp Image, Set Quality & Save Image to Disk: Large Version Dim MediumGraphic As Graphics = Graphics.FromImage(tempMedium) MediumGraphic.CompositingQuality = CompositingQuality.HighQuality MediumGraphic.SmoothingMode = SmoothingMode.HighQuality MediumGraphic.InterpolationMode = InterpolationMode.HighQualityBicubic Dim MediumRectangle = New Rectangle(0, 0, Medium_Size, Medium_Size) MediumGraphic.DrawImage(Original_Image, MediumRectangle, xm, ym, am, am, GraphicsUnit.Pixel) tempMedium.Save(ULdir_Medium & NewImageName, ImageFormat.Jpeg) 'Calculate Aspect/Size: Medium Version Dim tempSmall As System.Drawing.Image = New Bitmap(Small_Size, Small_Size) Dim a = Math.Min(Original_Width, Original_Height) Dim x = (Original_Width - a) / 2 Dim y = (Original_Height - a) / 2 'Create Temp Image, Set Quality & Save Image to Disk: Large Version Dim SmallGraphic As Graphics = Graphics.FromImage(tempSmall) SmallGraphic.CompositingQuality = CompositingQuality.HighQuality SmallGraphic.SmoothingMode = SmoothingMode.HighQuality SmallGraphic.InterpolationMode = InterpolationMode.HighQualityBicubic Dim SmallRectangle = New Rectangle(0, 0, Small_Size, Small_Size) SmallGraphic.DrawImage(Original_Image, SmallRectangle, x, y, a, a, GraphicsUnit.Pixel) tempSmall.Save(ULdir_Small & NewImageName, ImageFormat.Jpeg) 'Clean Up tempLarge.Dispose() LargeGraphic.Dispose() tempMedium.Dispose() MediumGraphic.Dispose() tempSmall.Dispose() SmallGraphic.Dispose() Original_Image.Dispose() Dim myID As Integer If IsNumeric(Request.QueryString("i")) Then myID = Request.QueryString("i") myID = Convert.ToInt32(myID) Else myID = Convert.ToInt32("0") End If Dim connStr As String = ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString Dim DBConn As New SqlConnection(connStr) Dim DBCmd As New SqlCommand Dim sql As String = "in_photo_SP" Dim myImage As String = NewImageName & ".jpg" DBConn.Open() Try DBCmd = New SqlCommand(sql, DBConn) DBCmd.CommandType = CommandType.StoredProcedure DBCmd.Parameters.Add("@TableID", SqlDbType.Int).Value = myID DBCmd.Parameters.Add("@photo", SqlDbType.NVarChar).Value = myImage DBCmd.Parameters.Add("@caption", SqlDbType.NVarChar).Value = caption.Text DBCmd.ExecuteNonQuery() Catch exp As Exception Response.Write(exp) End Try DBCmd.Dispose() DBConn.Close() DBConn = Nothing Else lblMessage.Text = "Please only upload .jpg files" lblMessage.Visible = True End If Else lblMessage.Text = "No file selected" lblMessage.Visible = True End If End Sub End ClassI have two problems with the code: 1) I look in the server once for a file with same name and if one exist, i use a new name. But really, I should keep looking until instead of just using the new one, just in case the new already exists to. Anyway to loop this until it finds a name is not using? 2) I'm getting an error on line 95 and 112:
I get the error on intellisense: Overload resolution failed because no accessible DrawImage can be called without a narrowing conversion. ..... Argument matching parameter 'srcX' narrows from 'Double' to Inter... narrows from 'Double' to 'Single'.
Any Ideas?
Thanks!
upload images drawimage
nilsan
All-Star
16940 Points
3719 Posts
Re: Look for file with same name, save to server.
Jan 29, 2011 08:03 AM|LINK
Check your calculation, and debug it. As per the error it may happen because
http://msdn.microsoft.com/en-us/library/xxa04k6c%28v=VS.80%29.aspx
Blog | Get your forum question answered | Microsoft Community Contributor 2011
Diomedes
Member
202 Points
145 Posts
Re: Look for file with same name, save to server.
Jan 29, 2011 05:26 PM|LINK
I wish I could check my calculation. But it isn't really my calculation, i use another code as base, so I have no idea how it actually works.