将图像转换为流

时间:2012-03-30 14:28:47

标签: asp.net image amazon-s3 image-resizing imageresizer

我正在使用一个上传图片,获取流并使用imageresizer.net调整大小的函数,然后将流上传到Amazon S3。

现在我想拍一张本地照片并将其转换为流。 (调整大小并上传到amazonS3)。基本上,如何将图像转换为流。

这可能是一个简单的问题,无法在任何地方找到答案。

这是一些基本代码。

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

3 个答案:

答案 0 :(得分:5)

[免责声明 - 我是ImageResizing.NET library OP的作者提出的问题。]

伙计 - 如果可以避免使用Bitmap和Image实例,请不要使用它。有a giant list of pitfalls会导致服务器崩溃。如果没有服务器安全的包装,请不要在System.Drawing中使用ANYTHING。

@dash - 你的代码几乎是正确的,除了内存泄漏。

安全地解码和编码图像 并不简单。让ImageResizing.Net库处理它。

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

如果您制作多个版本,请不要将某些内容加载到Bitmap或Image实例中。将文件克隆到MemoryStream中。

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

答案 1 :(得分:3)

以下代码段应该可以执行您想要的操作:

  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

顺便说一句,您可能会发现在调用方法时更容易参数化并完成所有工作。以下内容可能会让您的生活更轻松(假设您发布的代码是您实际使用的代码而非演示代码):

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

答案 2 :(得分:2)

读取图像字节,然后将其包装在MemoryStream

MemoryStream ms = new MemoryStrean(imageBytes);