Base64 decoding of a image gives a red question mark - c#

I am encoding and decoding facebook thumbnail image data using base64 strings in unity3d. When the user does not have a profile picture, the resulting image is a red question mark. Is there any way to recognise that an invalid picture is being sent so that I can replace it with a default pic of my choosing?
I am converting the string to image data using Convert.FromBase64String (string encoded) in c#.

I assume that you use some API to retrieve the base64 encoded string from an URL?
In that case, you could just dump the retrieved string once to the console and then copy it into your source code and compare this to the string you get in the future. Of course, this will break if the facebook API you use decides to deliver a different icon, in which case you would have to dump the new "unknown user" thumbnail.
string encoded = ... // however you obtain your thumbnail
print encoded; // dump the string to the console once. remove this statement later
if (encoded == "...here comes the (rather large) string you just copied")
encoded = "...here comes some other image you like to use, encoded as string";
...
Not very elegant, but at least easy to implement.

I suppose, if an error image (e.g. the "red question-mark" image) is predictably returned when no profile picture exists, you could simply test for that case and replace with another image of your choosing. You may choose to store the red question-mark image as a resource and then compare its Base64 string to the Base64 of each image returned from your requests.
In that case, the pivotal bit of code might look something like this (assuming you've already stored the image's Base64 string in a resource):
ResourceManager rm =
new ResourceManager("ExampleAppData", typeof(ExampleApp).Assembly);
String errorImageBase64 = rm.GetString("ErrorImageBase64");
// the image you get from your request
String resultImageBase64 = GetProfileImageBase64();
Image missingProfile;
if(resultImageBase64.Equals(errorImageBase64))
{
missingProfile = ImageFromBase64String(rm.GetString("MissingProfileBase64"));
}
else
{
missingProfile = ImageFromBase64String(resultImageBase64);
}
References:
http://msdn.microsoft.com/en-us/library/2xsy4hac.aspx
http://ozgur.ozcitak.com/snippets/2009/12/21/base64-encoding-an-image-with-csharp.html

In my experience it was due to a wrong request.
In my case the wrong API query returning Red Questionmark was /{fbId}/picture?g&type=square&height=128&width=128
I solved removing "?g", the working query now is
/{fbId}/picture?type=square&height=128&width=128

Related

Convert Base64 String to an Image File and Save it to File System?

I am trying to make a .Net 6 console application that would take in a base64string and then save it to the file system as an actual image file
Example
I have this image
https://cdn.pixabay.com/photo/2016/03/28/12/35/cat-1285634_960_720.png
I would have this image already as a base64 string.
Now I want to save to my file system as "cat-1285634_960_720.png"
I just can't figure out how to do it. All the examples I see say to use Image.Save() but I can't find that in .Net6 and looks like it is removed.
First convert the base64 string to a byte array and then use File.WriteAllBytes(...) to save it:
byte[] imageByteArray = Convert.FromBase64String(base64String);
File.WriteAllBytes("image.png", imageByteArray);

EvilDicom Image Corrupted

I'm trying to use the library to anonymize a dicom image.
In some cases it works fine, but in other cases the final image is corrupted. I mean, the pixels of the image are wrong and also the FileMetaInformationGroupLength tag of final image has changed.
This also happen when I don't anonymize the image, I just read and write the image in a new file.
This is my code:
//---------------------------------------------------------------------
string dir = #"C:\Users\Desktop\CT.dcm";
var dcmBytes = System.IO.File.ReadAllBytes(#dir);
try
{
var dcm = EvilDICOM.Core.DICOMObject.Read(dcmBytes);
var refName = new EvilDICOM.Core.Element.PersonName
{
FirstName = "",
Tag = EvilDICOM.Core.Helpers.TagHelper.PATIENT_NAME
};
dcm.ReplaceOrAdd(refName);
dcm.Write(#"C:\Users\Desktop\CT2.dcm");
}
catch(Exception ex)
{
throw new Exception("EXCEPTION: " + ex.Message);
}
//---------------------------------------------------------------------
Following is the original image with which I have the problem:
https://www.dropbox.com/s/s5ase23jl9908jm/3DSlice1.dcm?dl=0
Following is the screenshot with the original image and the final image (the corrupted image).
https://www.dropbox.com/s/12liy3gbw7dkb4d/Image_corrupted.PNG?dl=0
I don't know what is happening with the pixel data. But I have seen that the FileMetaInformationGroupLength tag changes.
Original image is compressed with Transfer syntax 1.2.840.10008.1.2.4.70. The output image is encoded with 1.2.840.10008.1.2 which means uncompressed. You need to check if this change in transfer syntax is applied correctly while calling dcm.Write.
Or may be that the transfer syntax is already changed while read operation.
In any case, make sure the change in transfer syntax is intended, it is done correctly and consistent with DICOM tags.
I never used Evil DICOM toolkit so I may not help you with code.

C# Parameter is not Valid Exception

I am trying to get Avatar from google talk.
I received packet from goole talk server like:
<presence from="xxxxxxxxxxxxx#gmail.com/MessagingA3e8c9465" to="xxxxxxxxxx#gmail.com/Jabber.NetF5D1AB65">
<show>away</show>
<caps:c ver="1.1" node="http://www.android.com/gtalk/client/caps" xmlns:caps="http://jabber.org/protocol/caps" />
<x xmlns="vcard-temp:x:update">
<photo>6373f2ccdf12ef06292ca2257dc0bdc9aa1040c2</photo>
</x>
I thought the hex vale of '<photo>' tag is the avatar (display image) of the contact. (Please correct me if I am wrong.)
I converted that value to byte[]
and used following code to display the image.
pictureBox1.Image = Image.FromStream(new MemoryStream(byte_array));
// byte_array is byte[] converted from hex value.
It raises exception saying:
Parameter is not valid.
I am using the following function to covert from hex to byte[]:
private static byte[] HexString2Bytes(string hexString)
{
int bytesCount = (hexString.Length) / 2;
byte[] bytes = new byte[bytesCount];
for (int x = 0; x < bytesCount; ++x)
{
bytes[x] = Convert.ToByte(hexString.Substring(x * 2, 2), 16);
}
return bytes;
}
I tries many ways but same result.
I also tried to convert the hex value to uppercase, but no luck, same result.
I am using .net 3.5 on windows 8.1 machine.
Thanks
Updated:
Thanks to every one for their comments and answer.
I was wrong the hex value was not avatar (display image).
I sent 'iq' request to server and it gives the avatar.
Thanks a lot.
Happy Coding.
http://www.xmpp.org/extensions/xep-0153.html says following:
Next, the user's client computes the SHA1 hash of the avatar image data itself (not the base64-encoded version) in accordance with RFC 3174 [4]. This hash is then included in the user's presence information as the XML character data of the child of an element qualified by the 'vcard-temp:x:update' namespace, as shown in the following example:
Example 3. User's Client Includes Avatar Hash in Presence Broadcast
So, basically hex value of '' tag is not the avatar, but SHA1 hash of the avatar image.
The hex value that you see is not the display image of the contact. It is a hash of the display image. The logic to get the display image is as follows.
After login on the XMPP client, you start receiving presence messages from the XMPP server.
In the presence message, you receive the hash of the avatar.
Check your local storage, if you have a binary image against the received hash.
If you have a binary image against the hash, then display the avatar on your client from the local storage.
If you do not have a binary image against the hash, send a request for v-card to the XMPP server, for the user against which you received the presence.
On receiving the v-card response, you will find the hash and the display image binary. Store this in some local storage.
For details on the XMPP packets Read section 3.2 on http://www.xmpp.org/extensions/xep-0153.html#retrieve
According to this, the photo is Base64-encoded. So you simple need to call Convert.FromBase64String to get the byte array from the photo element InnerText.

Using C# to convert base30 image from jsignature

I need some help about .net method to convert jsignature exported data.
I have an Android app sending base30 compressed data (for example "quaok1C2S3Q3W3A2A2I1Uk1y800Z6c1y1y1W2G2w2C2u1O1M1wie800Y2ccm1A1C1Q1S1U2G2y2M1O1u1y4424681y1C1Q2M2Q2w2A2woce_dC200Z2688408ccoeiamiga800000Y48gic1u1ucmaes866402000Z224420000000000002")
and a .Net web service (rest) receving data in order to save them to database and do some business.
At the moment I'm experimenting some problem in converting data from base30 to image.
As example, I'm referring to the SignatureDataConversion_dotNet code in the "extras" folder of the jsignature zip (https://github.com/willowsystems/jSignature) in order to do the conversion and manage data.
But something is not working for me...or, maybe, I've not completely understood how to manage base30 signature in .Net
This is a piece of the code I'm using:
Base30Converter conv = new Base30Converter();
int[][][] bytearray = conv.GetData(base30_string_from_app);
string actual = jSignature.Tools.SVGConverter.ToSVG(bytearray);
var byteArray = Encoding.ASCII.GetBytes(actual);
using (var stream = new MemoryStream(byteArray))
{
var svgDocument = SvgDocument.Open(stream);
var bitmap = svgDocument.Draw();
bitmap.Save("D:\prova.png", ImageFormat.Png);
}
But the image I get seems to be a "partion" of the whole signature sent, with only some strokes.
I've also checked the string I am manipulating: it is the same of what the app is sending.
As confirmation of this I succed in importing it on a html canvas, using jSignature "setData" method.
Thanks in advance for any help
As another example of the probable issue with the base30 conversion to svg in .Net, here we have the svg conversion of a signature exportet from http://willowsystems.github.io/jSignature/#/demo/
[svg]
[https://] dl.dropboxusercontent.com/u/77767500/SVG_from_jsignature_site.svg
this is the base 30 conversion of the same sign:
7yf1Ul1H4232121Z4577dabdllhhne1uf6423Y2587ddn1v1x1G1A1D1wpordd8431Z1338ffppkm1v1Db964210Y158b46ffgqkhqfef8673523_3EZ2519332455410Y315329745ba9me5343421200100000Z22345233345323311000Y142853746743121000Z10041112224332_jVZ746ba553200Y12368cabceb8a74200Z234a4ebcebfdbc65Y68afjdjd9f864334_4SZ110000Y3368745322000000Z1123355584323113000000Y31Z101101201001221
Trying to convert it using
Base30Converter conv = new Base30Converter();
int[][][] bytearray = conv.GetData(stream_to_convert);
string actual = jSignature.Tools.SVGConverter.ToSVG(bytearray);
i get:
[https://] dl.dropboxusercontent.com/u/77767500/SVG_from_jsignature_lib.svg
As you can see the two "svg" conversion seems to be different.
So, maybe, there is something not working properly in the jSignature.Tools for .Net ...
It took me a lot of trial and error with the .NET tools included in the jSignature package, but here's how I eventually got it to generate a PNG from a base30 jSignature string. I'm using VB .NET and returning a System.Drawing.Image (which you can output however you want):
Public Function GetSignaturePNG() As System.Drawing.Image
'jSignature conversion from base30 to SVG
Dim base30Converter As New jSignature.Tools.Base30Converter()
Dim arrBase30Data As Integer()()() = base30Converter.GetData(Me.SigBase30) 'our original base30 string
Dim strSVG As String = jSignature.Tools.SVGConverter.ToSVG(arrBase30Data)
'first convert it to SVG
Dim mySVG As SvgDocument
Dim newStream As New System.IO.MemoryStream(System.Text.ASCIIEncoding.[Default].GetBytes(strSVG))
mySVG = SvgDocument.Open(newStream, Nothing)
'then convert it to PNG
Dim tempStream As New System.IO.MemoryStream()
mySVG.Draw().Save(tempStream, System.Drawing.Imaging.ImageFormat.Png)
Dim img As System.Drawing.Image = System.Drawing.Image.FromStream(tempStream)
Return img
End Function
Update 6.20.2014: Leo, I was able to successfully export your base30 signature string (listed in your original question) as both SVG and PNG. And I got the entire "leo" signature, not just the few broken strokes. So I'm not sure what we're doing differently. Are you sure you're passing in the entire base30 string to the function?
The SVGConverter class uses string.Format to output decimals to the string. If your current culture is e.g. set to German, it will output fractions with a comma instead of points. So, add CultureInfo.InvariantCulture as the first argument to each String.Format in that class to make sure the SVG output is formatted with decimal points.

c# how to write a jpg image from request.binaryread

I have a flash app which sends raw data for a jpg image to a particular url Send.aspx . In Send.aspx I am using request.binaryread() to get the total request length and then read in the data to a byte array.
Then I am writing the data as jpg file to the server. The code is given below:
FileStream f = File.Create(Server.MapPath("~") + "/plugins/handwrite/uploads/" + filename);
byte[] data = Request.BinaryRead(Request.TotalBytes);
f.Write(data, 0, data.Length);
f.Close();
The file is getting created but there is no image in it. It always shows up as empty in any graphic viewer. What part am I missing. Am I supposed to use jpg encoding first before writing it to file? Thanks in advance
Well, you should use a using statement for your file stream, but other than that it looks okay to me.
A few suggestions for how to proceed...
Is it possible that the client isn't providing the data properly? Perhaps it's providing it as base64-encoded data?
Have you already read some data from the request body? (That could mess things up.)
I suggest you look closely at what you end up saving vs the original file:
Are they the same length? If not, which is longer?
If they're the same length, do their MD5 sums match?
If you look at both within a binary file editor, do they match at all? Any obvious differences?

Categories