Uploading and viewing an image.

mike55

Contributor
Joined
Mar 26, 2004
Messages
727
Location
Ireland
Hi all

My code for uploading and compressing an image is working, in so far that it uploads the image and compresses the image. The problem that I am encountering is how do I go about displaying that image on another page once the system redirects the user to the viewing page. I have tried using a image control from asp.net, however I am running into major difficulty with regards to the location of the image file. The second problem that I am having is with the deleting of the uploaded images, however I believe that that problem is to do with asp.net and permissions.

All suggestions for the first problem are welcome.

Mike55.
 
Hey Mike;
I think you can give a name for each uploaded picture and show it on different pages. For example i'm using product code as the name of pictue (sorry if i could not understand your problem :p )

For deleting a file, you can use an externat Dll file, i'm not sure but it may pass security.

Another one , I also need a code to compress images. Could you share it?
Thanks in advance
 
Appreciate the reply, managed to solve the problem regarding the problem regarding viewing the images once they have been uploaded. I assumed, and wrongly, that since the web application was running on a web server that if I said my file location was C:\temp, that the code would be referreing to the folder temp on the C drive of the server. To view the images, I had to create a new virtual directory on the web server, and store all images in a folder within that directory, and then referring to each image using the following address: http://(ip_address)/myImages/Pictures/44.jpg etc.

In relation to the compression code, this is what I am using:
Code:
Imports System.IO
Imports System.Drawing
Imports System.Drawing.Imaging
Imports System.Drawing.Graphics
Imports System.Configuration

Private Function CompressImage(ByVal currentLocation As String, ByVal newLocation As String, ByVal fileName As String) As Boolean
            CompressImage = True

            Dim bImage As Bitmap
            Dim codecs As ImageCodecInfo()
            Dim imageInfo As ImageCodecInfo
            Dim quality As Int16

            Try
                quality = System.Configuration.ConfigurationManager.AppSettings.Item("CompressionRate")
                bImage = CType(Image.FromFile(currentLocation), Bitmap)
                codecs = ImageCodecInfo.GetImageEncoders
                imageInfo = Nothing

                For Each codec As ImageCodecInfo In codecs
                    If codec.MimeType = "image/jpeg" Then
                        imageInfo = codec
                    End If
                Next

                Dim ep As EncoderParameters = New EncoderParameters

                ' Compress the image and save it to a new location.
                ep.Param(0) = New EncoderParameter(Encoder.Quality, CType(quality, Long))
                bImage.Save(newLocation & fileName.ToString & ".jpg", imageInfo, ep)
                bImage.Dispose()
                ep.Dispose()


                Return CompressImage
            Catch ex As Exception
                CompressImage = False
            End Try
        End Function

I have declared a variable quality, in my web.config file that value is set to 50, however, you can change it to either 25, 75, or 100 if necessary. If you happen to be uploading thumbnails, I would suggest that you firstly put a check in place regarding the image file size, and only compress if the file is too big otherwise let it be.

In the event that you get some better code for the compression, I would be interested in seeing it.

Mike55.
 
Happy to learn that you managed it.
But still i could not solve my problem. I want to create a small picture from a large one by decreasing sizes.
How can i do that?
I used your code by appyling 75 to compression paramether but the size become a bit higher! (142kb to 146kb) :confused:
 
aliassce said:
Happy to learn that you managed it.
But still i could not solve my problem. I want to create a small picture from a large one by decreasing sizes.
How can i do that?
I used your code by appyling 75 to compression paramether but the size become a bit higher! (142kb to 146kb) :confused:

Yea I had experienced a similar problem on a number of images within a particular size range. I don't think that the code that I posted previously is the best for compressing images, and I am on the look out for better code.

Mike55.
 
Solved

I found it al last. First we create a new image object from saved image and then take it into a new bitmap. Then save the bitmap. That's it
I'M very happy :D
Code:
  dim path as string='image path
  Dim img As System.Drawing.Image = System.Drawing.Image.FromFile(path)
  Dim btmp As New System.Drawing.Bitmap(img, 20, 30)'new sizes
        btmp.Save(path)
 
Back
Top