-
Creating an image from the memorystream
I have to created a function which compresses an image, but rather than save
it to a physical file I want to save it to memory and return the stream as
an image object from the function. How would you do this? can this be done?
' make a memory stream to work with the image bytes
Dim imgStream As New MemoryStream()
'Save the image with a filename that indicates
' the compression quality used
oImg.Save(imgStream, imgICI, encoderParams)
' make byte array the same size as the image
Dim imageContent(imgStream.Length) As Byte
'rewind the memory stream
imgStream.Position = 0
'load the byte array with the image
imgStream.Read(imageContent, 0, CInt(imgStream.Length))
Dim bw As New BinaryWriter(imgStream)
'******* Error Generated ****************
Return CType(bw.BaseStream(), Image)
'*****************************************
Thanks in advance
Paul
-
Re: Creating an image from the memorystream
Paul,
> ' make byte array the same size as the image
> Dim imageContent(imgStream.Length) As Byte
Keep in mind that the number within the parentheses indicates the
upper bound of the array, not the length, so you're allocating one
more byte than you need here. It should be
Dim imageContent(imgStream.Length - 1) As Byte
> Dim bw As New BinaryWriter(imgStream)
> '******* Error Generated ****************
> Return CType(bw.BaseStream(), Image)
> '*****************************************
That should probably be
Return Image.FromStream(imgStream)
The BinaryWriter object isn't needed AFAICT.
But the new Image object you return will probably uncompress the data
from the stream into its internal format, so I don't see the point in
doing this. You could replace the entire procedure with
Return oImg.Clone()
Mattias
--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/
Please reply only to the newsgroup.
-
Re: Creating an image from the memorystream
Worked a treat. Thanks
Mattias Sjögren <mattias.dont.want.spam@mvps.org> wrote:
>Paul,
>
>> ' make byte array the same size as the image
>> Dim imageContent(imgStream.Length) As Byte
>
>Keep in mind that the number within the parentheses indicates the
>upper bound of the array, not the length, so you're allocating one
>more byte than you need here. It should be
>
>Dim imageContent(imgStream.Length - 1) As Byte
>
>
>> Dim bw As New BinaryWriter(imgStream)
>> '******* Error Generated ****************
>> Return CType(bw.BaseStream(), Image)
>> '*****************************************
>
>That should probably be
>
>Return Image.FromStream(imgStream)
>
>The BinaryWriter object isn't needed AFAICT.
>
>But the new Image object you return will probably uncompress the data
>from the stream into its internal format, so I don't see the point in
>doing this. You could replace the entire procedure with
>
>Return oImg.Clone()
>
>
>
>Mattias
>
>--
>Mattias Sjögren [MVP] mattias @ mvps.org
>http://www.msjogren.net/dotnet/
>Please reply only to the newsgroup.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks