Get image to bytearray

R

RichardF

Can anyone tell me what is wrong with this code?

I want to get the ocntents of the JPEG image into a byte array. After
the theStream.read call, the theBytes array is still filled with all
nulls. and iCount is zero.

I used this article
(http://msdn.microsoft.com/library/d.../html/frlrfsystemiomemorystreamclasstopic.asp)
as a starting point.

Thanks for any help

RichardF



Imports System
Imports System.IO
Imports System.Text
Imports System.Drawing

Module Module1

Sub Main()

Dim theImage As Image
Dim theStream As New MemoryStream
Dim theBytes As Byte()
Dim iCount As Integer

Try
theImage = Image.FromFile("C:\Images\Test.jpg")
theImage.Save(theStream, Imaging.ImageFormat.Jpeg)
theBytes = New Byte(theStream.Length - 1) {}
iCount = theStream.Read(theBytes, 0, theStream.Length - 1)
Catch ex As Exception
End Try

End Sub

End Module
 
S

Steve C. Orr [MVP, MCSD]

The most likely cause of the problem is security. By default the ASPNET
user account does not have access to that directory. Either grant it access
or have ASP.NET run under a different user account by using impersonation.

For example, you can add a line similar to this to your web.config file:
<identity impersonate="true" userName="domain\MyAppUser">
password="password"/>

Here's more info on impersonation:
http://msdn.microsoft.com/library/d...-us/cpguide/html/cpconaspnetimpersonation.asp
 
R

RichardF

Although this will eventually be in a web service (hence the ASP.NET
group), currently I am testing the code in a console application.
Therefore I don't have a web.config and it should be running under the
currently logged in operator, not ASP.NET.

Any other ideas?

Thanks.

RichardF
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top