Parameter Not valid Image Conversion - c#

Code:
Image imgnew = null;
using (var ms1 = new MemoryStream(img))
{
imgnew = Image.FromStream(ms1);
}
Getting a Parameter No valid while trying to convert binary to image
Read a lot of solution most of them claiming the byte is incorrect where as I generated the code from
this site http://codebeautify.org/base64-to-image-converter
and the byte code represents the correct image
Thanks
UPDATE:
Sorry for the unclear question earlier, was running out of time
As of now I do not have the exact Code but im writing the steps
Receiving a String
Converting it to byte array using Encoding.ASCII.GetBytes(base64String)
and then passing the bye array to the above code

Turned Out To Be an Encoding issue by:
Encoding.ASCII.GetBytes(base64String)
Sorted Out by changing it to :
Convert.FromBase64String(base64String)
Hope this might be of some help to someone else.

Related

How can I download a video using VideoLib when it's Async?

Alright, here is my dilemma:
I wanted to learn how to use NuGet, so I tested it out using a system called VideoLibrary(https://www.nuget.org/packages/VideoLibrary/). I successfully got it installed to my computer, and was finally got it working inside of code. I was able to successfully download a video, which is the purpose of the extension, however it was in the wrong format. So, having read through the questions and answers section, I got this code:
var youTube = YouTube.Default;
var allVideos = await youTube.GetAllVideosAsync(URL);
var mp4Videos = allVideos.Where(_ => _.Format == VideoFormat.Mp4);
Now, from what my understanding is, that first downloads all the videos, and then sets the variable to only the MP4 video.
However, after that is done, I don't know how to save it to a byte array. Typically, to save a video using VideoLib, you have to assign it to a byte array and then save the byte array to a file. So I used this code previously with a video that wasn't async:
byte[] bytes = video.GetBytes();
var stream = video.Stream();
File.WriteAllBytes(#"C:\" + fullName, bytes);
Now, this method doesn't work with my videos now because they're async. THe developer mentioned later that you could assign the video to a byte array by using:
byte[] bytes = allVideos.GetBytesAsync();
However, that doesn't work for some reason.
I'm assuming I'm using it wrong and that's why, but I don't know.
The code is underlined in red, and it gives:
'IEnumerable<YouTubeVideo>' does not contain a definition to 'GetBytesAsync'
and no extension method 'GetBytesAsync accepting a first argument of type
'IEnumerable<YouTubeVideo>'coulb be found
Any help would be appreciated.
Okay, so, I've discovered the answer to my question:
The problem is that allVideos is multiple videos. When you use the method GetAllVideosAsync it retrieves a whole list of videos. You have to narrow it down.
When you use mp4Videos you've narrowed it down to one video, however the computer still doesn't know that because it's a variable of multiple videos.
So, simply put the code through this:
var SingleVideo = mp4Videos.FirstOrDefault();
Then, using the SingleVideo variable, you can save it to a byte array like a conventional video, but using the async method:
Byte[] Byte = await SingleVideo.GetBytesAsync();
Once the contents are saved to the byte array, you can save it any way you please.
I hope this helps anyone who needs it!

Saving and retrieving an image from database in c#

I've got a c# application which saves and displays records, When saving a record with an image using the picture box it seems as though the image has saved just fine but in the database all the images seem to have the same tag
[BLOB - 13B]
when saving the exact same image into the database manually using the dbms the image tag will read
[BLOB - 2.4KB]
with this tag I can use my image display method and it will be fine
The image is being converted or something when being saved but I cant seem to understand what is the problem.
here is the code for selecting and saving the image
MemoryStream ms = new MemoryStream();
pictureBox.Image.Save(ms, pictureBox.Image.RawFormat);
byte[] img = ms.ToArray();
because of the way this code changes the image, when retrieving it, I receive an error which says "parameter is invalid"
using c# and a mysqldatabase, although the database type is irrelevant.
Anyone understand what is going wrong?
It sounds like you're saving a byte array of your image to your database and need to know how to convert that byte array back into an image so that it can be displayed. You can do that with a function like this:
public Image ConvertByteArrayToImage(byte[] byteArray)
{
MemoryStream ms = new MemoryStream(byteArray, 0, byteArray.Length);
ms.Write(byteArray, 0, byteArray.Length);
Image image = Image.FromStream(ms, true);
return image;
}
UPDATE: After reading it over again. I don't think I answered the actual question you were asking. It sounds like your not converting your image into a byte array. I would try taking the image from your picturebox and putting it into an Image object. Then converting that Image object into a byte array. Rather than saving picturebox.Image.RawFormat
For any future readers.
I figured out my own problem after some research, turns out that when I was concatenating the SQL string and placing the byte array into it, the byte array would be changed to follow the string parameters. Hence the blob in the database would say [Blob 13B] only rather than the 2.7KB it should have been
To avoid this, you have to use a "parameterized" SQL query and not a standard concatenated one.
see Why my BLOB field is still 13B - what I am doing wrong?
and SQL Insert Query Using C#

Can't figure out where these C# and Java code differ

I have some C# code that converts an image to base64 string. The code is :
MemoryStream ms = new MemoryStream();
Image img = Image.FromFile(filename);
img.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
string s = Convert.ToBase64String(ms.GetBuffer());
I am trying to implement it with JAVA. my java code is :
BufferedImage img = null;
img = ImageIO.read(new File(filename));
byte[] bytes = ((DataBufferByte)img.getData().getDataBuffer()).getData();
String js = Base64.encodeBase64String(bytes);
this two piece of code should return the same string for the same image file. But they are returning different strings. I am unable to figure out why. Can anyone shed some light on it?
this two piece of code should return the same string for the same image file
No, they really shouldn't.
The C# code is returning the base64 representation of a JPEG-encoded version of the image data - and potentially some 0s at the end, as you're using GetBuffer instead of ToArray. (You want ToArray here.)
The Java code is returning the base64 representation of the raw raster data, according to its SampleModel. I'd expect this to be significantly larger than the string returned by the C# code.
Even if both pieces of code encoded the image with the same format, that doesn't mean they'll come up with the exact same data - it will depend on the encoding.
Importantly, if you just want "the contents of the file in base64" then you don't need to go via an Image at all. For example, in C# you could use:
string base64 = Convert.ToBase64String(File.ReadAllBytes(filename));
The fact that it's an image is irrelevant in that respect - the file is just a collection of bytes, and you can base64-encode that without understanding the meaning of those bytes at all.

Convert image to string (base64) in metro/windows 8 .NET 4.5

I need to convert an picture (that is stored in an object of type Image) to a string for storage (and later for conversion back into an Image object for display) in a metro app
I have found lots of answers for converting an image to a base64 string in .NET 4.0 etc but in 4.5 the System.Windows.Bitmap namespace isn't there (the Image class is in Windows.UI.Xaml.Media.Imaging) and the method that was in that namespace that made it possible in 4.0 "Save()" doesn't seem to be in 4.5...unless I just can't find it.
Theres an example of doing this here but like I said it doesn't work in a metro app/.NET 4.5
any ideas?
more details:
the method that will do this will convert an instance field that contains an image object (ive used its source property, is this correct?) and needs to store the resultant string from the conversion in an instance string field. this whole object can then be serialized, ignoring the Image field, with the hope of deserializing later and restoring the string to the Image field for display. so far ive tried to use a DataContractSerializer to serialize string from the image, but it doesn't seem to like it. Once I get a string from the image I would be able to serialize that, but its not something I've ever done before.
Also, it seems that the only .net 4.5 documentation that is definitely correct is the pages here: http://msdn.microsoft.com/library/windows/apps/
pages at the "normal looking" msdn site for .net 4.5 don't seem to always work in metro apps? (just a theory?)
[solved]
I finally got it! for anyone else that ever has to do this the answer is here: http://social.msdn.microsoft.com/Forums/en-US/winappswithcsharp/thread/38c6cb85-7454-424f-ae94-32782c036567/
I did this
var reader = new DataReader(myMemoryStream.GetInputStreamAt(0));
var bytes = new byte[myMemoryStream.Size];
await reader.LoadAsync((uint)myMemoryStream.Size);
reader.ReadBytes(bytes);
after this sequence, the byte array bytes will have the data from the stream in it, from there I set a string to the value of
Convert.ToBase64String(bytes);
I finally got it! for anyone else that ever has to do this the answer is here: http://social.msdn.microsoft.com/Forums/en-US/winappswithcsharp/thread/38c6cb85-7454-424f-ae94-32782c036567/
I did this
var reader = new DataReader(myMemoryStream.GetInputStreamAt(0));
var bytes = new byte[myMemoryStream.Size];
await reader.LoadAsync((uint)myMemoryStream.Size);
reader.ReadBytes(bytes);
after this sequence, the byte array bytes will have the data from the stream in it, from there I set a string to the value of
Convert.ToBase64String(bytes);
I'm not sure of this because I do not have the .net 4.5 installed here, but I think this could work:
You could use the BitmapSource.CopyPixels() method to extract the pixels of the image:
http://msdn.microsoft.com/en-us/library/ms616043(v=vs.110).aspx
Then use Convert.ToBase64String() to do the convertion.
Also, here are some useful imaging HOW-TOs:
http://msdn.microsoft.com/en-us/library/ms750864(v=vs.110)
Try BitmapEncoder. Example of how to create a BitmapEncoder here. The appropriate namespace is Windows.Graphics.Imaging.
BitmapEncoder gets you an encoder. You can then use GetPixelDataAsync(BitmapPixelFormat, BitmapAlphaMode, BitmapTransform, ExifOrientationMode, ColorManagementMode) to get your pixel data. Following that you can use any generic C# base64 encoder.
(Examples are Javascript but should work for C# as well since the classes exist in C#)
You should store the encoded format of image (say JPEG) format, decoded back to byte[], create a MemoryStream, then Metro BitmapImage can be created from the stream.

C#: Converting byte[] to UTF8 encoded string

I am using a library called EXIFextractor to extract metadata information from images. This lib in part is using System.Drawing.Imaging.PropertyItem to do all the hard work. Some of the data in PropertyItem, such as Image Details etcetera, are fetched as an ASCII-string stored in a byte[] according to the Microsoft documentation.
My problem is that international characters (å, ä, ö, etcetera) are dropped and replaced by questionmarks. When I debug the code it is apparent that the byte[] is a representation of an UTF-8.
I'd like to parse the byte[] as an UTF8-string, how can I do this without loosing any information in the process?
Thanks in advance!
Update:
I have been asked to provide a snippet from my code:
The first snippet is from the class I use, namely the EXIFextractor.cs written by Asim Goheer
foreach( System.Drawing.Imaging.PropertyItem p in parr )
{
string v = "";
// ...
else if( p.Type == 0x2 )
{
// string
v = ascii.GetString(p.Value);
}
And this is my code where I try my best to handle the results of the above.
try {
EXIFextractor exif = new EXIFextractor(ref bmp, "");
object o;
if ((o = exif["Image Description"]) != null)
MediaFile.Description = Tools.UTF8Encode(o.ToString());
I have also tried a couple of other ways of getting my precious å, ä, ö from the data, but nothing seems to do the trick. I am starting to think Hans Passant is right about his conclusions in his answer below.
string yourText = System.Text.Encoding.UTF8.GetString(yourByteArray);
Use the GetString method on the Encoding.UTF8 object.
Yes, this is a problem with the app or camera that originated the image. The EXIF standard has horrible support for text, it has to be encoded in ASCII. That only ever works out well when the photographer speaks English. No doubt the software that encoded the image is ignoring this requirement. Which is what the PropertyItem class is doing as well, it encodes a string to byte[] with Marshal.StringToHGlobalAnsi(), which assumes the system's default code page.
There's no obvious fix for this, you'll get mojibake when the photo was made too far away from your machine.
Maybe you could try another encoding? UTF16, Unicode?
If you aren't sure if it got encodes right in the first place try to view the exif metadata with another exif reader.

Categories

Resources