Using PostedFile.InputStream twice - asp.net

I'm trying to resize one image to 5 different sizes (and then upload them to amazonS3).
I'm using imageresizer.net
the problem seems to be that i cannot use the inputstream twice. it works the first time.
Dim SmallStream As Stream = New MemoryStream
Dim TinyStream As Stream = New MemoryStream
If FileUpload1.HasFile Then
**ImageResizer.ImageBuilder.Current.Build(FileUpload1.PostedFile.InputStream, SmallStream, New ResizeSettings("maxwidth=100&maxheight=100"))
ImageResizer.ImageBuilder.Current.Build(FileUpload1.PostedFile.InputStream, TinyStream, New ResizeSettings("maxwidth=100&maxheight=100"))**
AmazonUploadFile("SmallImages/" & FileUpload1.FileName, SmallStream)
AmazonUploadFile("TinyImages/" & FileUpload1.FileName, TinyStream)
End If
Public Shared Function GetS3Client() As AmazonS3
Dim appConfig As NameValueCollection = ConfigurationManager.AppSettings
Dim s3Client As AmazonS3 = AWSClientFactory.CreateAmazonS3Client(AWS_ACCESS_KEY, AWS_SECRET_KEY)
Return s3Client
End Function
Public Sub AmazonUploadFile(S3Key As String, FileStream As Stream)
Dim request As New PutObjectRequest()
request.WithBucketName(BUCKET_NAME)
request.WithKey(S3Key).InputStream = FileStream
request.WithCannedACL(S3CannedACL.PublicRead)
GetS3Client.PutObject(request)
End Sub
The code breaks when i try to retrieve the FileUpload1.PostedFile.InputStream the second time.

I believe ImageResizer will accept Image objects to that method. So, you could read the InputStream into an Image object first (Image.FromStream()), then you can use that image object repeatedly.

[Disclaimer: I'm the author of http://imageresizing.net/]
Pass FileUpload1.PostedFile instead of FileUpload1.PostedFile.InputStream to the ImageResizer and it will automatically handle re-seeking the stream to the beginning after each read. (Make sure you're using 3.1.5 or later).
Alternatively, use the ImageJob class and set ResetSourceStream=true.

Related

FileUpload file to Azure Blob Storage

I have a System.Web.UI.WebControls.FileUpload control that passes both Word and PDF files that need to be stored in Azure Blob Storage.
From the Code Behind page it passes to the common library to manage Azure Functions:
Private Sub UploadButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles UploadButton.Click
Dim fileExt As String = String.Empty
Dim newGuid As New Guid
Dim fileName As String
Dim documentType As Document.DocumentType
Page.Validate()
newGuid = Guid.NewGuid
If Page.IsValid() AndAlso Me.FileUploadNewDoc.HasFile Then
Try
'Test that MIME is either msword of pdf
If FileUploadNewDoc.PostedFile.ContentType.Contains("msword") Then
fileExt = "doc"
documentType = Document.DocumentType.LeaseWordDoc
ElseIf FileUploadNewDoc.PostedFile.ContentType.Contains("pdf") Then
fileExt = "pdf"
documentType = Document.DocumentType.LeasePDF
Else
fileExt = "na"
End If
If fileExt <> "na" Then
fileName = newGuid.ToString & "." & fileExt
AzureStorage.SaveBlob(FileUploadNewDoc.FileContent, fileName, mDocumentContainer, mStorageConnectionString)
End If
Catch ex As Exception
' Handle Error
Finally
FileUploadNewDoc.Dispose()
End Try
End If
End Sub
The AzureStorage.SaveBlob code:
Public Function SaveBlob(ByRef fileContent As Stream,
ByVal fileName As String,
ByVal containerName As String,
ByVal storageConnectionString As String) As Boolean
Dim storageAccount As CloudStorageAccount = CloudStorageAccount.Parse(storageConnectionString)
Dim blobClient As CloudBlobClient = storageAccount.CreateCloudBlobClient()
Dim container As CloudBlobContainer = blobClient.GetContainerReference(containerName)
Dim blockBlob As CloudBlockBlob = container.GetBlockBlobReference(fileName)
Using fileContent
fileContent.Position = 0
blockBlob.UploadFromStream(fileContent)
End Using
Return True
End Function
My questions:
Is this best way to take the File that has been uploaded and save it to Azure Blob Storage?
Am I handling the Stream correctly? I'm passing ByRef and have a Using statement around the usage.
Should I be setting content type explicitly when saving it to storage? If so how do I do that?
Note, I normally code in C# so an example in C# is fine if you're not familiar with VB.NET.
Is this best way to take the File that has been uploaded and save it
to Azure Blog Storage?
The best way depends on your use case. If it is just small files you're OK. If you want to support large files you might want to do chunked uploading. You can take blocks of 1 megabyte which you can upload separately or in parallel. Once you are done uploading all the blocks you commit the file and it is stiched together in Azure Blob storage. Look at CloudBlockBlob.PutBlock and CloudBlockBlob.PutBlockList.
Am I handling the Stream correctly? I'm passing ByRef and have a Using
statement around the usage.
You are but if you want to support larger files you might want to upload with JavaScript and create two endpoint to receive chunks and to commit after all chunks are sent. There are multiple libraries that can help you.
Should I be setting content type explicitly when saving it to storage? If so
how do I do that?
If you upload files that you want to embed in HTML it's wise to have a content type. If you want the links to the file to be download links you don't have to. Although it can never hurt.
blockBlob.Properties.ContentType = "image/jpeg";

How to Edit Excel with EPPLUS and send it as attachment

i have the following code, which it is working, it opens a file stream from an embedded Xlsx saves it on a memorystream then pass the value to the attachment and send it with no problem.
Private Sub SendFile2()
Dim path As String = Server.MapPath("~/App_Data/RQM.xlsx")
Using fileST As FileStream = IO.File.OpenRead(path)
Dim memStream As New MemoryStream()
memStream.SetLength(fileST.Length)
fileST.Read(memStream.GetBuffer(), 0, CInt(fileST.Length))
'' Code for MailMessage and SMTP goes here
MailMsg.Attachments.Add(New Attachment(memStream, "myFile.xlsx", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"))
End Using
End Using
End Sub
Now im trying to do some edition (adding some values to that file, but still working in a memorystream) but i am having some problems
i've added
Using pck As ExcelPackage = New ExcelPackage(memStream)
Dim xlswb As ExcelWorkbook = pck.Workbook
Dim xlsws As ExcelWorksheet = xlswb.Worksheets.First
xlsws.Cells(20, 4).Value = "HELLO WORLD"
pck.SaveAs(memStream)
End Using
Which when i put breakpoints i see pck getting the value of MemStream with a lenght of 83084 (the value of the original file) 81kb xlsx, when i add the value to the cell the lenght of memStream is 162143 so it seems it does some modifications, however when i send again the memstream trough
MailMsg.Attachments.Add(New Attachment(memStream, "myFile.xlsx", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"))
i receive a file of 165b, and i'm not sure what i am doing wrong.
Any help would be apreciated
Edit:
Well i wasn't able to save it with the same memStream so instead i did this.
memStream2 = New MemoryStream(pck.GetAsByteArray())
MailMsg.Attachments.Add(New Attachment(memStream2, "myFile2.xlsx", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"))
And that worked as a charm. Hope is useful to anyone.

XmlSerializer adding extra characters

I have a method that serializes an object to a string, exhibit a:
Shared Function Serialize(ByVal o As Object) As String
Dim rtnVal As String = ""
Dim x As New System.Xml.Serialization.XmlSerializer(o.GetType())
Using memStream As New MemoryStream
Dim stWriter As New System.IO.StreamWriter(memStream)
x.Serialize(stWriter, o)
rtnVal = Encoding.UTF8.GetString(memStream.GetBuffer())
End Using
Return rtnVal
End Function
Using this serialized data, I'm now inserting it into an XML typed field in my SQL 2012 database. Most of the time, this code works very well, but for a particular object, I'm getting "invalid" characters, namely the error "parsing line 5 character 17 illegal xml character". I took a look at my data, and it's clean, as you can see here:
<?xml version="1.0" encoding="utf-8"?>
<RatingDetails xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<LenderName>dsfg</LenderName>
<VehiclePrice>345</VehiclePrice>
</RatingDetails>
Some snooping led me do the IsXMLChar method - http://msdn.microsoft.com/en-us/library/system.xml.xmlconvert.isxmlchar%28v=vs.100%29.aspx - and using this I was able to loop through each character in my serialized XML string. Low and behold, I DO have invalid data. I have 15 "" character's at the end of my string - WTF!?!
So my questions to you all are, where the heck are the extra "'s coming from, why cant I see them when I inspect the string in my quick watch, and how do I prevent em in the first place.
Yours in ASP.NET, ewitkows
The problem is you are calling MemoryStream.GetBuffer. According to the MSDN article:
Note that the buffer contains allocated bytes which might be unused. For example, if the string "test" is written into the MemoryStream object, the length of the buffer returned from GetBuffer is 256, not 4, with 252 bytes unused. To obtain only the data in the buffer, use the ToArray method; however, ToArray creates a copy of the data in memory.
To fix it, you could call ToArray instead:
Shared Function Serialize(ByVal o As Object) As String
Dim rtnVal As String = ""
Dim x As New System.Xml.Serialization.XmlSerializer(o.GetType())
Using memStream As New MemoryStream
Dim stWriter As New System.IO.StreamWriter(memStream)
x.Serialize(stWriter, o)
rtnVal = Encoding.UTF8.GetString(memStream.ToArray())
End Using
Return rtnVal
End Function
However, that's still not really efficient. If the stream contains a lot of data, it's going to copy the whole thing into a new array for no reason. For peace of mind, I would recommend using the StreamReader to read the MemoryStream rather than trying to decode it yourself (but don't forget to seek back to the beginning of the stream before reading it):
Public Function Serialize(ByVal o As Object) As String
Dim rtnVal As String = ""
Dim x As New System.Xml.Serialization.XmlSerializer(o.GetType())
Using memStream As New MemoryStream
Dim stWriter As New System.IO.StreamWriter(memStream)
x.Serialize(stWriter, o)
Dim reader As New StreamReader(memStream)
memStream.Position = 0 ' Seek to start of stream
rtnVal = reader.ReadToEnd()
End Using
Return rtnVal
End Function

Converting image into stream

I'm using a function that uploads an image, takes the stream and resizes it using imageresizer.net, then uploads the stream to Amazon S3.
Now I want to take a local picture and convert it into a stream. (to resize and upload to amazonS3). Basically, how do you convert an image into a stream.
This might be a simple question, just could not find the answer anywhere.
Here is some basic code.
Public Shared Sub MoveToAmazon(strImg As String, SKU As String)
Dim fullImg As String = "C:\ImageLocation\" & strImg
Dim img As Image = Image.FromFile(fullImg)
'Here Im missing the code to convert it to a stream.
UploadImage(imgStream, SKU)
End Sub
Public Shared Sub UploadImage(imgStream As Stream, imgName As String)
Dim MainStream As Stream = New MemoryStream
Dim HomeStream As Stream = New MemoryStream
Dim SmallStream As Stream = New MemoryStream
Dim TinyStream As Stream = New MemoryStream
Dim MidStream As Stream = New MemoryStream
Dim GridStream As Stream = New MemoryStream
Dim ListStream As Stream = New MemoryStream
Dim c As New ImageResizer.Configuration.Config
Dim SourceImage As Bitmap = New Bitmap(imgStream)
Dim SourceMain As Bitmap = New Bitmap(SourceImage)
Dim SourceHome As Bitmap = New Bitmap(SourceImage)
Dim SourceSmall As Bitmap = New Bitmap(SourceImage)
Dim SourceTiny As Bitmap = New Bitmap(SourceImage)
Dim SourceMid As Bitmap = New Bitmap(SourceImage)
Dim SourceGrid As Bitmap = New Bitmap(SourceImage)
Dim SourceList As Bitmap = New Bitmap(SourceImage)
ImageResizer.ImageBuilder.Current.Build(SourceMain, MainStream, New ResizeSettings("width=300&height=372&scale=both&paddingWidth=40")) 'ProductPage
ImageResizer.ImageBuilder.Current.Build(SourceHome, HomeStream, New ResizeSettings("width=112&height=147&scale=both")) 'HomePage Products
ImageResizer.ImageBuilder.Current.Build(SourceGrid, GridStream, New ResizeSettings("width=149&height=149&scale=both")) 'Categories Grid
ImageResizer.ImageBuilder.Current.Build(SourceList, ListStream, New ResizeSettings("width=171&height=206&scale=both")) 'Categories List
ImageResizer.ImageBuilder.Current.Build(SourceSmall, SmallStream, New ResizeSettings("width=64&height=75&scale=both")) 'Accessories
ImageResizer.ImageBuilder.Current.Build(SourceTiny, TinyStream, New ResizeSettings("width=82&height=82&scale=both")) 'Cart
ImageResizer.ImageBuilder.Current.Build(SourceMid, MidStream, New ResizeSettings("width=155&height=116&scale=both")) 'CategoryMain
AmazonUploadFile("OriginalImages/" & imgName, imgStream)
AmazonUploadFile("MainImages/" & imgName, MainStream)
AmazonUploadFile("HomeImages/" & imgName, HomeStream)
AmazonUploadFile("GridImages/" & imgName, GridStream)
AmazonUploadFile("ListImages/" & imgName, ListStream)
AmazonUploadFile("SmallImages/" & imgName, SmallStream)
AmazonUploadFile("TinyImages/" & imgName, TinyStream)
AmazonUploadFile("MidImages/" & imgName, MidStream)
End Sub
Public Shared Sub AmazonUploadFile(S3Key As String, FileStream As Stream)
Dim request As New PutObjectRequest()
request.WithBucketName(BUCKET_NAME)
request.WithKey(S3Key).InputStream = FileStream
request.WithCannedACL(S3CannedACL.PublicRead)
GetS3Client.PutObject(request)
End Sub
[Disclaimer - I'm the author of the ImageResizing.NET library the OP is asking the question about.]
Folks - do NOT use Bitmap and Image instances if you can possibly avoid it. There is a giant list of pitfalls that will crash your server. Do NOT use ANYTHING from System.Drawing without a server-safe wrapper around it.
#dash - Your code is almost right, aside from the memory leaks.
Decoding and encoding images safely isn't straightforward. Let the ImageResizing.Net library handle it.
Dim settings as New ResizeSettings("width=64&height=75&scale=both")
Using ms As New MemoryStream()
ImageBuilder.Current.Build("C:\ImageLocation\" & strImg, ms, settings)
ms.Seek(0, SeekOrigin.Begin)
UploadImage(ms, SKU)
End Using
Never load something into a Bitmap or Image instance if you're making multiple versions. Clone the file into a MemoryStream instead.
Using fs as New FileStream(...)
Using ms as MemoryStream = Util.StreamUtils.CopyStream(fs)
'For loop here with your setting variations
ms.Seek(0, SeekOrigin.Begin)
'Place upload and resize code here
'End Loop
End Using
End Using
The following code snippet should do what you want:
Using myImage = Image.FromFile(fullImg)
Using ms As New MemoryStream()
myImage.Save(ms, ImageFormat.Jpeg)
ms.Seek(0, SeekOrigin.Begin)
UploadImage(ms, SKU)
End Using
End Using
As an aside, you might find it easier to parameterize your methods and do all the work when calling them. Something like the following may make your life easier (this assumes the code you posted is code you are actually using and not a demo):
Public Shared Sub UploadImages()
'Call this for each image
MoveToAmazon("C:\ImageLocation\blah.jpg", "OriginalImage", 300, 300, 0, "whatever")
End Sub
Public Shared Sub MoveToAmazon(strImg As String, targetFolder As String, height as Integer, width as Integer, padding as Integer, SKU As String)
Dim fullImg As String = "" & strImg
Using img = Image.FromFile(fullImg)
'Here Im missing the code to convert it to a stream.
Using ms As New MemoryStream()
Image.Save(ms, ImageFormat.Jpeg)
ms.Seek(0, SeekOrigin.Begin)
UploadImage(ms, SKU)
End Using
End Using
End Sub
Public Shared Sub UploadImage(imgStream As Stream, imgName As String, targetFolder As String, height as Integer, width as Integer, padding as Integer, SKU As String)
Dim c As New ImageResizer.Configuration.Config
ImageResizer.ImageBuilder.Current.Build(SourceMain, imgStream, New ResizeSettings("width=" & CStr(width) & "&height=" & CStr(height) & "&scale=both&paddingWidth=" & CStr(padding))
AmazonUploadFile(targetFolder & "/" & imgName, imgStream)
End Sub
Public Shared Sub AmazonUploadFile(S3Key As String, FileStream As Stream)
Dim request As New PutObjectRequest()
request.WithBucketName(BUCKET_NAME)
request.WithKey(S3Key).InputStream = FileStream
request.WithCannedACL(S3CannedACL.PublicRead)
GetS3Client.PutObject(request)
End Sub
Using ms As New MemoryStream()
Image.Save(ms, ImageFormat.Jpeg)
ms.Seek(0, SeekOrigin.Begin)
UploadImage(ms, SKU)
End Using
Read the image bytes and then you wrap it in a MemoryStream
MemoryStream ms = new MemoryStream(imageBytes);

Why am I getting this generic, non-descript error in GDI+ when trying to save a PNG?

I have a function that dynamically adds text to an image in a predesignated spot. Originally I did it with jpegs, and it was working. I switched to PNG so the images would be better quality, as the original jpegs were kind of pixely. Anyway, here is my code. It executes down to the oBitmap.Save(), then dies with "A General Error Has Occurred in GDI+".
Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
context.Response.ContentType = "image/png"
context.Response.Clear()
context.Response.BufferOutput = True
Try
Dim oText As String = context.Server.HtmlDecode(context.Request.QueryString("t"))
If String.IsNullOrEmpty(oText) Then oText = "Placeholder"
Dim oPType As String = context.Server.HtmlDecode(context.Request.QueryString("p"))
If String.IsNullOrEmpty(oPType) Then oPType = "none"
Dim imgPath As String = ""
Select Case oPType
Case "c"
imgPath = "img/banner_green.png"
Case "m"
imgPath = "img/banner_blue.png"
Case Else
Throw New Exception("no ptype")
End Select
Dim oBitmap As Bitmap = New Bitmap(context.Server.MapPath(imgPath))
Dim oGraphic As Graphics = Graphics.FromImage(oBitmap)
Dim frontColorBrush As New SolidBrush(Color.White)
Dim oFont As New Font(FONT_NAME, 30)
Dim oInfo() As ImageCodecInfo = ImageCodecInfo.GetImageEncoders
Dim oEncoderParams As New EncoderParameters(2)
Dim xOffset As Single = Math.Round((oBitmap.Height - oFont.Height) / 2, MidpointRounding.ToEven)
Dim oPoint As New PointF(275.0F, xOffset + 10)
oEncoderParams.Param(0) = New EncoderParameter(Encoder.Quality, 100L)
oEncoderParams.Param(1) = New EncoderParameter(Encoder.ColorDepth,8L)
oGraphic.TextRenderingHint = Drawing.Text.TextRenderingHint.AntiAlias
oGraphic.DrawString(oText, oFont, frontColorBrush, oPoint)
oBitmap.Save(context.Response.OutputStream, oInfo(4), oEncoderParams)
context.Response.Output.Write(oBitmap)
oFont.Dispose()
oGraphic.Dispose()
oBitmap.Dispose()
context.Response.Flush()
Catch ex As Exception
End Try
End Sub
The only changes I made to this from the jpeg version are:
context.Response.ContentType = "image/jpeg" changed to "image/png"
changed base images (img/banner_green.jpg, img/banner_blue.jpg) to .png
added the second encoding parameter specifying color depth
changed oInfo(1) (jpeg) to oInfo(4) (png)
Are there more things I need to tweak to get this routine to properly generate the PNG?
According to this post, Bitmap.Save requires a seekable stream to save as PNG, which HttpResponse.OutputStream isn't. You'll have to save the image into a MemoryStream first, and then copy the contents of it to Response.OutputStream, like:
Dim tempStream as New MemoryStream
oBitmap.Save(tempStream, ImageFormat.Png, oEncoderParams)
Response.OutputStream.Write(tempStream.ToArray(), 0, tempStream.Length)
Also note that the line
context.Response.Output.Write(oBitmap)
does something different then what you are probably expecting. HttpResponse.Output is a TextWriter, and the overload you use here, TextWriter.Write(object) will just call ToString on the object and write the results into the stream, what in this case results in writing "System.Drawing.Bitmap" to the output.
You're disposing of the bitmap before you're flushing the Response? Try flipping that around. Also, it looks like you're writing the bitmap to the stream twice. I'm not sure why you're doing that. Save the bitmap to the output stream or use the Write method of the Response object, but not both.

Resources