How to Simply 'Read' a File That Is in Use

Transform byte array in Image returns Invalid Argument exception

When saving binary data into sql server using the varbinary datatype, the SqlDataReader or similar classes already know how to get the byte array.

No need to convert the varbinary to a string...

You should use a code like that:

        using (SqlCommand cmd = new SqlCommand("select template from dbo.EnrollDigital where id=4", sqlConnection))
{
byte[] templateData = (byte[])cmd.ExecuteScalar();
Image img = byteArrayToImage(templateData);
}

ArgumentException when calling System.Drawing.Image.FromStream()

Looking at the MSDN page for Image.FromStream, it states that the parameter is either null or contains an invalid image when that exception is thrown. Can you also send a hash of the image data to the server, which it could then use to validate your image data hasn't been corrupted in transit? If it is corrupt the server could notify the client that it should resend the image. I suspect that you are running so much data that the odds are you are receiving an occasional corrupt bit.

Also add a temporary check just to make sure your socketData.dataBuffer hasn't somehow been set to null while you are debugging this issue.

Try/catch Image.FromStream() without catching the rest of the using statement?

It's not the using that's important... it's the call to IDisposable.Dispose(). using makes that call for you, behind the scenes, allowing you to simply write this:

using(var x = new X())
{
// do stuff with x
}

... instead of this:

X x = null;
try
{
x = new X();
// do stuff with x
}
finally
{
if(x != null)
{
x.Dispose(); // clean up resources
}
}

It's a nice way to skip a bunch of boiler-plate code but you're by no means forced to use it. You can explicitly call IDisposable.Dispose() and leave yourself with more control over what happens where:

public void ActionMethod()
{
// declare image...
Image image = null;

try
{
// attempt to load image from stream...
image = Image.FromStream(httpPostedFileBase.InputStream)
}
catch
{
// failed to load image from stream...
ModelState.AddModelError( "", "File is not a valid image" );
// exit
return;
}

try
{
// perform additional processing...
MoreStuffToDo(image);
}
catch
{
// handle errors from MoreStuffToDo()
}
finally
{
// clean up image...
image.Dispose();
}
}

Error Parameter is not valid while converting Bytes into Image

try this

public Image byteArrayToImage(byte[] byteArrayIn)
{
System.Drawing.ImageConverter converter = new System.Drawing.ImageConverter();
Image img = (Image)converter.ConvertFrom(byteArrayIn);

return img;
}

Image.FromStream: Parameter not valid

FromStream is expecting data in one of these formats:

Managed GDI+ has built-in encoders and decoders that support the following file types:

  • BMP
  • GIF
  • JPEG
  • PNG
  • TIFF

Your byte array I suspect is not in these and does not have the metadata or compression information each of these formats expects.

What you want to do is create a Bitmap object and read through each pixel in the byte array, calling SetPixel in the bitmap for the appropriate pixel. You'll end up with a Bitmap (which is an Image) that has the pixels you want.

Parameter is not valid Exception

Given that it's an ArgumentException it's to do with one of the arguments passed to the FromStream method.

If you open the second of the two links above you'll see from the documentation that an ArgumentException is raised when the stream passed does not represent a valid image format (you can confirm this by checking the exception's ParamName property.

So that means the format of the image that is being uploaded is not supported by the Image class. Either that, or the bytes of the image are being screwed in some way. That would seem to be supported by your own code - where you replace the '+' with ' ' in the base 64 string. Base64 is not intended to have spaces in it - take that line of code out.

Update

Since you say it doesn't work without it - I'm guessing the data is being passed in a manner where an incoming '+' is being interpreted as a space and that's why you're trying to reinstate them. If sent in a request body this shouldn't happen, so since it is I'm guessing the iPhone app and your server need to use Modified base64 for URLs instead.

Unable to create System.Drawing.Image object from WebResponse Stream

Resetting the stream position does not work on the Stream base class. If you can read the response into a MemoryStream, then you will be able to manipulate the image as you please. Something like this could work:

using (WebResponse response = await request.GetResponseAsync())
{
using (Stream responseStream = response.GetResponseStream())
{
using (MemoryStream memStream = new MemoryStream())
{
responseStream.CopyTo(memStream);
// some code that calls a 3rd party image resizer, passing in the original stream
ResizeImage(memStream, out resizedOutputStream);
memStream.Seek(0, SeekOrigin.Begin);

// manipulation of the resizedOutputStream

// now i want to create an image from the ORIGINAL stream
using (Image image = Image.FromStream(memStream))
{

}
}
}
}


Related Topics



Leave a reply



Submit