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;
}
System.Drawing.Image Parameter Not Valid
Figured it out.
It turns out that the data is backwards...due to FlatBuffers serialization.
The solution is to reverse the order of my for-loop
during serialization:
for (var i = message.ImageBytes.Length; i -->0;)
{
builder.AddByte(message.ImageBytes[i]);
}
System.Drawing Parameter is not valid
You are calling livePreview.Image.Dispose()
in the UI thread, but you're not setting livePreview.Image
to null afterwards.
So between the time that you dispose livePreview.Image
and the time that you assign a new image to it, livePreview.Image
points to a disposed object.
So I think occasionally your picturebox tries to draw itself during this time, and fails when it attempts to access its (disposed) Image property.
The solution would be:
if (livePreview.Image != null)
{
//Dispose the resources
this.Invoke(new MethodInvoker(delegate() {
livePreview.Image.Dispose();
livePreview.Image = null;
}));
}
Or even better, assign the new image in the same step:
var newFrame = (Bitmap)eventArgs.Frame.Clone();
this.Invoke(new MethodInvoker(delegate() {
if (livePreview.Image != null)
{
livePreview.Image.Dispose();
}
livePreview.Image = newFrame;
}));
In general, you need to understand what's happening with your various Bitmap objects. Anything that keeps a reference to a Bitmap that has been disposed will be a problem.
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.
Parameter is not valid when loading image into picturebox
Dispose current image to prevent the OutOfMemory
exception and assign the image loaded with a new instance of the Bitmap
class to preserve the Image
:
pbImage.Image?.Dispose();
using (var image = System.Drawing.Image.FromStream(fs))
{
pbImage.Image = new System.Drawing.Bitmap(image);
}
Related Topics
What Advantages of Extension Methods Have You Found
C# Console Receive Input with Pipe
Change Route Collection of MVC6 After Startup
How to Ensure a Form Displays on the "Additional" Monitor in a Dual Monitor Scenario
What Is the Impact of Thread.Sleep(1) in C#
Setting/Getting the Class Properties by String Name
Use Xml Includes or Config References in App.Config to Include Other Config Files' Settings
How to Delete a Directory with Read-Only Files in C#
Async Process Start and Wait for It to Finish
How to Add Claims in ASP.NET Identity
Excel to Datatable Using Epplus - Excel Locked for Editing
Determine What Control the Contextmenustrip Was Used On
How to Use Use Late Binding to Get Excel Instance
Is There a Lower Bound Function on a Sortedlist<K ,V>
Compare Equality Between Two Objects in Nunit