04. januar 2010 - 12:21Der er
8 kommentarer og 1 løsning
FileUpload: Ændre size
Før i læser spørgsmålet kan jeg lige så godt sige at det ikke kan løses ved at ændre maxRequestLength I web.config. Jeg ønsker at manipulere med selve filen.
hvis du skal manipulere med filstørrelsen og samtidig gemme hele billedet skal du manipulere med selve billedet - fx ved at resize det eller pille ved kvaliteten. Du kan se et lille eksempel her; http://www.4guysfromrolla.com/articles/012203-1.aspx
Jeg har i mellemtiden fundet ud af at resize mit billede, men jeg kan ikke få det nye billede gemt i min db. Jeg lånte et eksempel fra en blog, hvor den gemmet en ny fil på serveren. Du kan se det jeg også har forsøgt at få denne til at gemme i DB istedet..
Private Sub ResizeImage() Const Lx = 200 ' max width for thumbnails Const Ly = 240 ' max height for thumbnails Const upload_dir = "images/upload_resize_test/" ' directory to upload file Const upload_original = "sample" ' filename to save original as (suffix added by script) Const upload_thumb = "thumb" ' filename to save thumbnail as (suffix added by script) Const upload_max_size = 25 ' max size of the upload (KB) note: this doesn't override any server upload limits Dim fileExt ' used to store the file extension (saves finding it mulitple times) Dim newWidth, newHeight As Integer ' new width/height for the thumbnail Dim l2 ' temp variable used when calculating new size Dim fileFld As HttpPostedFile ' used to grab the file upload from the form Dim originalimg As System.Drawing.Image ' used to hold the original image Dim msg ' display results Dim upload_ok As Boolean ' did the upload work ?
'Randomize() ' used to help the cache-busting on the preview images upload_ok = False If LCase(Request.ServerVariables("REQUEST_METHOD")) = "post" Then fileFld = Request.Files(0) ' get the first file uploaded from the form (note:- you can use this to itterate through more than one image) If fileFld.ContentLength > upload_max_size * 1024 Then msg = "Sorry, the image must be less than " & upload_max_size & "Kb" Else Try originalimg = System.Drawing.Image.FromStream(fileFld.InputStream) ' work out the width/height for the thumbnail. Preserve aspect ratio and honour max width/height ' Note: if the original is smaller than the thumbnail size it will be scaled up If (originalimg.Width / Lx) > (originalimg.Width / Ly) Then l2 = originalimg.Width newWidth = Lx newHeight = originalimg.Height * (Lx / l2) If newHeight > Ly Then newWidth = newWidth * (Ly / newHeight) newHeight = Ly End If Else l2 = originalimg.Height newHeight = Ly newWidth = originalimg.Width * (Ly / l2) If newWidth > Lx Then newHeight = newHeight * (Lx / newWidth) newWidth = Lx End If End If
Dim thumb As New Bitmap(newWidth, newHeight)
'Create a graphics object Dim gr_dest As Graphics = Graphics.FromImage(thumb)
' just in case it's a transparent GIF force the bg to white Dim sb = New SolidBrush(System.Drawing.Color.White) gr_dest.FillRectangle(sb, 0, 0, thumb.Width, thumb.Height)
'Re-draw the image to the specified height and width gr_dest.DrawImage(originalimg, 0, 0, thumb.Width, thumb.Height)
Try ''********Inserting image****************
'stream = thumb.PostedFile.InputStream 'Dim uploadedFile(stream.Length) As Byte
' Housekeeping for the generated thumbnail If Not thumb Is Nothing Then thumb.Dispose() thumb = Nothing End If Catch msg = "Sorry, that was not an image we could process." End Try End If
' House Keeping ! If Not originalimg Is Nothing Then originalimg.Dispose() originalimg = Nothing End If
'If upload_ok = True Then ' Response.Write("Upload OK") 'Else LblMessage.Text = msg 'End If
På faktisk samme måde som du gør i den originale kode, altså lave en Byte-variabel og læse streamen af det resized billede der ind i stedet for streamet af det uploadede billede.
Jeg skal ikke have hjælp til at lave den reelle kode, det er mere fordi jeg ikke ved hvilke parametre der gør at jeg kan læse streamen fra det resized foto
Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.