Hi, Can someone please help me .... i'm really struggling with something that should be very simple ... i found soe code which I have modified slightly but things aren't working the files don't get uploaded and converted. If I do a simlpe upload using Postedfile.saveas
it works fine ... but I need to convert the image first. when i try this it fails - the code is failing at the part where I create the 'objImage' which is a Image/bitmap. PLEASE CAN SOMEONE TAKE A LOOK AT MY CODE AND HELP ME ... ITS VERY URGENT :-O I need
to do the following 1)upload an image of some sort (doesn' matter what type) 2)before uploading convert it to jpg 3) save it to the server 4) create and save a smaller image used for thumb. ... at the moment am having probs with the simple conversion ... but
as you can see from the commented code I plan to add another smaller copy of the converted image to the server also. If anyone could help me ... especially on the first part I would very much appreciate it. I have included my code and some of the things I
have tried to help you understand the prob. Many Thanks, Nitin P.S. Sorry bout any spell errors etc ... very tired burning the midnight oils. MY FORM CODE:
<form id="Form1" method="post" encType="multipart/form-data" runat="server">
Choose Your File To Upload
<input id="File1" type="file" name="File1" runat="server">
Upload_Click event handler is fired... --%>
</form>
--------------------------
CODE BEHIND:
Public Class uploadtest
Inherits System.Web.UI.Page
#Region " Web Form Designer Generated Code "
'This call is required by the Web Form Designer.
Private Sub InitializeComponent()
End Sub
Protected WithEvents doUpload As System.Web.UI.WebControls.Button
Protected WithEvents File1 As System.Web.UI.HtmlControls.HtmlInputFile
Protected WithEvents lblResultSuc As System.Web.UI.WebControls.Label
Protected WithEvents frmConfirmation As System.Web.UI.WebControls.Panel
Protected WithEvents lblResultUnSuc As System.Web.UI.WebControls.Label
Protected WithEvents Button1 As System.Web.UI.WebControls.Button
'NOTE: The following placeholder declaration is required by the Web Form Designer.
'Do not delete or move it.
Private designerPlaceholderDeclaration As System.Object
Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub
#End Region
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
End Sub
Sub doUpload_Click(ByVal s As Object, ByVal e As EventArgs)
' add validation of inputs else
' if IsValid () Then
If Not (File1.PostedFile Is Nothing) And (File1.PostedFile.ContentLength > 0) Then
Dim fn As String = System.IO.Path.GetFileName(File1.PostedFile.FileName)
Dim syspath As String = Server.MapPath("/")
Dim myapppath As String = "xrays\"
Dim myimagedirpath As String = "uploadimages\"
Dim MyDateTime As DateTime = DateTime.Now
Dim strMyDateTime As String = MyDateTime.ToString("ddmmyyhhmmss")
Dim SaveLocation As String = syspath & myapppath & myimagedirpath & strMyDateTime & "_" & fn 'or put & ".jpg" here
Dim ThumbSaveLocation As String = syspath & myapppath & myimagedirpath & strMyDateTime & "_" & "Thumb_" & fn 'or put & ".jpg" here
' get filename without extension & ".jpg" => Savelocation
Dim ssource As String = File1.PostedFile.FileName
Dim iMax As Integer = 200
Dim iWidth As Integer
Dim iHeight As Integer
Dim objImage As System.Drawing.Image 'tried chaning this to Image= error ,or bitmap
Dim objBitmap As Bitmap
Dim objGraphics As Graphics
Response.Write(SaveLocation & "
") 'for debugging
Response.Write(File1.PostedFile.FileName & "
") 'for debugging
Response.Write(ssource) 'for debugging
Try
'uncomment line below to go back to start -orginally working
'File1.PostedFile.SaveAs(SaveLocation)
Response.Write("hello")
'*****create bitmap object to manipulate and convert file to jpg
'*****IE WHERE PROBLEMS ARE THIS BITMAP/IMAGE OBJECT NOT BEING CREATED??!
objImage = New Bitmap(ssource)
'for debugging
Response.Write("Hello1")
objImage.Save(SaveLocation & ".JPG", Imaging.ImageFormat.Jpeg)
'for debugging
Response.Write("Hello3 done")
' save the location to database using insert statement later - not savelocation value
'''''create thumbnail image
' Calculate the thumbnails dimensions
' iWidth = iMax
' iHeight = (objImage.Height / objImage.Width) * iMax
' if wanted scale by height use following:
' iHeight = iMax
' iWidth = (objImage.Width / objImage.Height) * iMax
' Create a new bitmap object with the thumbnail's dimensions
' objThumb = new bitmap (iWidth, iHeight)
' Create a graphics object, used for resizing
' objGraphics = Graphics.FromImage(objThumb)
' Set the graphics object's InterpolationMode
'objGraphics.InterpolationMode = InterpolationModeHighQualityBicubic
' OR DO ....
'// Shrink the image using high-quality interpolation.
'objGraphics.SetInterpolationMode(InterpolationModeHighQualityBicubic)
'objGraphics.DrawImage(&image,Rect(290, 250, 0.6 * width, 0.6 * height), 0, 0,width,height, UnitPixel)
' Draw the actual thumbnail
'objGraphics.DrawImage(objImage, 0, 0, iWidth, iHeight)
'
'save thumbnail ...
'objBitmap.Save(ThumbSaveLocation, ImageFormat.Jpeg)
'SAVE ALL STRINGS OF SaveLocation and ThumbSaveLocation to database plus any other inputs
'i.e. Case Title, AuthorID=dropdown, CaseSummary
'all into database
'rectangle example
'Bitmap myBitmap(L"Spiral.png");
'Rect compressionRect(210, 10, myBitmap.GetWidth() / 2,
'myBitmap.GetHeight() / 2);
'myGraphics.DrawImage(&myBitmap, compressionRect);
lblResultSuc.Text = "The case was added successfully!
" & "The image has been uploaded successfully to: " & SaveLocation & "
The Thumbnail was created to: " & ThumbSaveLocation
frmConfirmation.Visible = "True"
Catch Exc As Exception
lblResultSuc.Text = "There was an adding the Case and the Image! Please Try Again.
" & "Error: " & Exc.Message
lblResultSuc.Visible = "True"
End Try
Else
Response.Write("Please select a file to upload!")
End If
'End If ... of the IsValid()
End Sub
Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Response.Redirect("AddEditQuest.aspx")
End Sub
End Class
__________________________________________________ TRIED FOLLOWING VARIATIONs of onjImage: Dim
objImage As Image 'gives error image not defined objImage = New Bitmap(sSource) -------- Dim objImage As System.Drawing.Image 'no error this time but still not work objImage = New Bitmap(sSource) ----------- Dim objImage As System.Drawing.Image 'does not work
objImage = System.Drawing.Image.FromFile(ssource) --------- Dim objImage As New System.Drawing.Image = System.Drawing.Image.FromFile(ssource) 'errors image not recog ---------- Dim objImage As Bitmap 'no error this time but still not work objImage = New Bitmap(sSource)
------------ ------- Dim objImage As New Image(System.Drawing.Image.FromFile(ssource)) - error Dim objImage As New System.Drawing.Image(System.Drawing.Image.FromFile(ssource)) - error Dim objImage As New Bitmap(System.Drawing.Image.FromFile(ssource)) - error
----- Dim objImage as new Image (ssource) 'returns error = image is not recognised Dim objImage as new System.Drawing.Image (ssource) 'returns error = image is not recognised Dim objImage As System.Drawing.Image objImage = new bitmap (System.Drawing.Image.FromFile(ssource))
... Also ried replacing ssource with File1.PostedFile.Filename
nitinmistry1...
Member
5 Points
1 Post
HELP PLEASE ... really urgent question about images/uploading/resizing/thumbs etc
Aug 20, 2003 05:49 AM|LINK