I'm trying to send a string contained a varbinary into an ImageView
string ImageHexAsString = "0xFFD8FFE000104A464946000101010...";//Here is my string as VarBinary
byte[] toBytes = Encoding.ASCII.GetBytes(ImageHexAsString); //Here i'm converting string to byte[]
Bitmap bitmap = BitmapFactory.DecodeByteArray(toBytes, 0, toBytes.Length);
imageView.SetImageBitmap(bitmap); //and here i'm send it to imageview
I get an empty white image nothing more.Is something wrong?
UPDATE: Just realized this is about c#, not Java. Answer is the same though. Possible duplicate of How do you convert a byte array to a hexadecimal string, and vice versa?
This is probably a duplicate of Convert a string representation of a hex dump to a byte array using Java?.
What's happening is that you shouldn't try and interpret the hex string as text (with Encoding.ASCII.GetBytes()), because it isn't, it's an image.
SOLVED:
ImageHexAsString = "0xFFD8FFE000104A4649460001010100....";
List<byte> byteList = new List<byte>();
string hexPart = ImageHexAsString.Substring(2);
for (int i = 0; i < hexPart.Length / 2; i++)
{
string hexNumber = hexPart.Substring(i * 2, 2);
byteList.Add((byte)Convert.ToInt32(hexNumber, 16));
}
byte[] original = byteList.ToArray();
Bitmap bitmap = BitmapFactory.DecodeByteArray(original, 0, original.Length);
imageView.SetImageBitmap(bitmap);
it seems to work with 256x256 resolution I dont know how to change the struck if image is bigger
Related
(I use Google Translate, sorry..)
I'm trying to do a small project with shorthand.
I found similar topics, but they are not what I need.
The bottom line is, I get an image at the input, convert it to byte [], then to binary. (Everything is OK here)
Then I translate the text to binary. (Everything is OK here)
in the binary array, I change the last two bits to the ones I need.
I translate everything back to byte[] (everything is OK Here)
and then I try to convert byte[] to Image and an error occurs (Incorrect argument)
If I do not make changes to binary, the conversion is successful ( and I get the same image that I gave at the input).
change code in the binary array:
public byte[] ShifrMessage(byte[] input, string message)
{
byte[] ConvertToByteArray(string str, Encoding encoding)
{
return encoding.GetBytes(str);
}
String ToBinary(Byte[] data)
{
return string.Join("", data.Select(byt => Convert.ToString(byt, 2).PadLeft(8, '0')));
}
//message in the binary
var binaryString = ToBinary(ConvertToByteArray(message, Encoding.ASCII));
byte[] ret = new byte[input.Length];
for (int i = 0; i < ret.Length; i++)
{
string a = Convert.ToString(ret[i], 2).PadLeft(8, '0');
//changing the last two bits
if (binaryString.Length >= 2)
{
a = a.Substring(0, a.Length - 2) + binaryString.Substring(0, 2);
binaryString = binaryString.Substring(2);
}
///
byte b = StringToByte(a);
ret[i] = b;
}
return ret;
}
I assume that when converting to Image, the integrity of the image that was changed is checked.
conversion:
public Image byteArrayToImage(byte[] byteArrayIn)
{
using (var ms1 = new MemoryStream(byteArrayIn))
{
return Image.FromStream(ms1);
}
}
Can you tell me what the problem might be?
I have a unicode character from the FontAwesome cheat sheet:
#xf042;
How do I put that character into c# ?
string s = "????";
I have tried entering it is as and using a .
If you just want a lighter version of what Darin posted to convert the hex value to a string containing the unicode position from the private area of the FontAwesome font, you can use this >>
private static string UnicodeToChar( string hex ) {
int code=int.Parse( hex, System.Globalization.NumberStyles.HexNumber );
string unicodeString=char.ConvertFromUtf32( code );
return unicodeString;
}
Just call it as follows >>
string s = UnicodeToChar( "f042" );
Alternatively, you can simply use the C# class with all the icons and loader pre-written here >> FontAwesome For WinForms CSharp
Assuming the hex input represents UTF8 encoded string you could have a function that will convert a HEX string:
public static string ConvertHexToString(string hex)
{
int numberChars = hex.Length;
byte[] bytes = new byte[numberChars / 2];
for (int i = 0; i < numberChars; i += 2)
{
bytes[i / 2] = Convert.ToByte(hex.Substring(i, 2), 16);
}
return Encoding.UTF8.GetString(bytes);
}
and then filter out the unnecessary characters from your input before feeding it to this function:
string input = "#xf042;";
string s = input.Replace("#x", string.Empty).Replace(";", string.Empty);
string result = ConvertHexToString(s);
Obviously you will need to adjust the correct encoding based on the input, because the hex simply represents a byte array and in order to decode this byte array back to a string you're gonna need to know the encoding.
This might be a simple one, but I can't seem to find an easy way to do it. I need to save an array of 84 uint's into an SQL database's BINARY field. So I'm using the following lines in my C# ASP.NET project:
//This is what I have
uint[] uintArray;
//I need to convert from uint[] to byte[]
byte[] byteArray = ???
cmd.Parameters.Add("#myBindaryData", SqlDbType.Binary).Value = byteArray;
So how do you convert from uint[] to byte[]?
How about:
byte[] byteArray = uintArray.SelectMany(BitConverter.GetBytes).ToArray();
This'll do what you want, in little-endian format...
You can use System.Buffer.BlockCopy to do this:
byte[] byteArray = new byte[uintArray.Length * 4];
Buffer.BlockCopy(uintArray, 0, byteArray, 0, uintArray.Length * 4];
http://msdn.microsoft.com/en-us/library/system.buffer.blockcopy.aspx
This will be much more efficient than using a for loop or some similar construct. It directly copies the bytes from the first array to the second.
To convert back just do the same thing in reverse.
There is no built-in conversion function to do this. Because of the way arrays work, a whole new array will need to be allocated and its values filled-in. You will probably just have to write that yourself. You can use the System.BitConverter.GetBytes(uint) function to do some of the work, and then copy the resulting values into the final byte[].
Here's a function that will do the conversion in little-endian format:
private static byte[] ConvertUInt32ArrayToByteArray(uint[] value)
{
const int bytesPerUInt32 = 4;
byte[] result = new byte[value.Length * bytesPerUInt32];
for (int index = 0; index < value.Length; index++)
{
byte[] partialResult = System.BitConverter.GetBytes(value[index]);
for (int indexTwo = 0; indexTwo < partialResult.Length; indexTwo++)
result[index * bytesPerUInt32 + indexTwo] = partialResult[indexTwo];
}
return result;
}
byte[] byteArray = Array.ConvertAll<uint, byte>(
uintArray,
new Converter<uint, byte>(
delegate(uint u) { return (byte)u; }
));
Heed advice from #liho1eye, make sure your uints really fit into bytes, otherwise you're losing data.
If you need all the bits from each uint, you're gonna to have to make an appropriately sized byte[] and copy each uint into the four bytes it represents.
Something like this ought to work:
uint[] uintArray;
//I need to convert from uint[] to byte[]
byte[] byteArray = new byte[uintArray.Length * sizeof(uint)];
for (int i = 0; i < uintArray.Length; i++)
{
byte[] barray = System.BitConverter.GetBytes(uintArray[i]);
for (int j = 0; j < barray.Length; j++)
{
byteArray[i * sizeof(uint) + j] = barray[j];
}
}
cmd.Parameters.Add("#myBindaryData", SqlDbType.Binary).Value = byteArray;
I'm having trouble loading an image "string" in the PictureBox control.
I am not able to find a way to charge it and do not know if it is possible to do it that way.
I tried the following code, but without success:
1st
var s= "0x89504E470D0A1A0A0000000D49484452000000900000005B0802000000130B9B9
8000000017352474200AECE1CE90000000467414D410000B18F0BFC6105000000097048597300000EC300000EC301C76FA8640000013B49444154785EEDD6410D00211443418470C4BF333C808977F8C9242B8094CE9675F7F10D4A600D3AABA3FE045CD8B01F8C0B736146B14C8030C2CA7E7977104618616502841156F6CB86114618616502841156F6CB86114618616502841156F6CB86114618616502841156F6CB86114618616502841156F6CB86114618616502841156F6CB86114618616502841156F6CB86114618616502841156F6CB86114618616502841156F6CB86114618616502841156F6CB86114618616502841156F6CB86114618616502841156F6CB86114618616502841156F6CB86114618616502841156F6CB86114618616502841156F6CB86114618616502841156F6CB86114618616502841156F6CB86114618616502841156F6CB86114618616502841156F6CB86114618616502841156F6CB86114618616502841156F6CB863DFFBF32F5C2C8C5B40000000049454E44AE426082";
picture.Image = Base64ToImage(s);
static Image Base64ToImage(string base64String)
{
byte[] imageBytes = Convert.FromBase64String(base64String);
MemoryStream ms = new MemoryStream(imageBytes);
return Image.FromStream(ms, true);
}
Can anyone help!?
This seems to be a red rectangle 91*144 if decoded properly.
remove 0x and space from string.
convert string to byte[] - I used converter found on StackOverFlow the one by CainKellye (How can I convert a hex string to a byte array?)
string s = "89504E470D0A1A0A0000000D49484452000000900000005B0802000000130B9B98000000017352474200AECE1CE90000000467414D410000B18F0BFC6105000000097048597300000EC300000EC301C76FA8640000013B49444154785EEDD6410D00211443418470C4BF333C808977F8C9242B8094CE9675F7F10D4A600D3AABA3FE045CD8B01F8C0B736146B14C8030C2CA7E7977104618616502841156F6CB86114618616502841156F6CB86114618616502841156F6CB86114618616502841156F6CB86114618616502841156F6CB86114618616502841156F6CB86114618616502841156F6CB86114618616502841156F6CB86114618616502841156F6CB86114618616502841156F6CB86114618616502841156F6CB86114618616502841156F6CB86114618616502841156F6CB86114618616502841156F6CB86114618616502841156F6CB86114618616502841156F6CB86114618616502841156F6CB86114618616502841156F6CB86114618616502841156F6CB86114618616502841156F6CB86114618616502841156F6CB863DFFBF32F5C2C8C5B40000000049454E44AE426082";
byte[] imageBytes = StringToByteArrayFastest(s);
MemoryStream ms = new MemoryStream(imageBytes);
Bitmap bmp = (Bitmap)Image.FromStream(ms);
pictureBox1.Image = bmp;
and the result is:
I don't think s is a base 64 string, it looks more like hexadecimal - it even still has 0x in front of it and the 'digits' are no higher than F. You should definitely remove the 0x in front. To get a base 64 string, you could use the code from this website (I haven't tested it):
public static string ConvertHexStringToBase64(string hexString)
{
if (hexString.Length % 2 > 0)
throw new FormatException("Input string was not in a correct format.");
if (Regex.Match(hexString, "[^a-fA-F0-9]").Success == true)
throw new FormatException("Input string was not in a correct format.");
byte[] buffer = new byte[hexString.Length / 2];
int i=0;
while (i < hexString.Length) {
buffer[i / 2] = byte.Parse(hexString.Substring(i, 2), System.Globalization.NumberStyles.HexNumber);
i += 2;
}
return Convert.ToBase64String(buffer);
}
currently I am testing a script that tries to save an image file converted from a string of HEX, however, when I try to execute the Save command, parameter not valid appears.
// Some junk hex image data
string hexImgData = #"FFD8FFE000104A46494600010200006400640000FFFFD9";
// Call function to Convert the hex data to byte array
byte[] newByte = ToByteArray(hexImgData);
MemoryStream memStream = new MemoryStream(newByte);
// Save the memorystream to file
Bitmap.FromStream(memStream).Save("C:\\img.jpg");
// Function converts hex data into byte array
public static byte[] ToByteArray(String HexString)
{
int NumberChars = HexString.Length;
byte[] bytes = new byte[NumberChars / 2];
for (int i = 0; i < NumberChars; i += 2)
{
bytes[i / 2] = Convert.ToByte(HexString.Substring(i, 2), 16);
}
return bytes;
}
Currently I am still in the process of looking what causes this, please advice.
As was mentioned in the comments, your bitmap format is wrong, all you have is some random hex data and the Bitmap.FromStream method has no idea what to do with it. If you look at this link which discusses how to create a bitmap file with a hex editor it discusses the BitmapHeader, BitmapInfoHeader, and the Pixel RGB Data. I was able to create a bitmap using your code by taking the data from their example and using it.
string bitmapHeader = "424D860000000000000036000000";
string bitmapInfoHeader = "280000000500000005000000010018000000000050000000C40E0000C40E00000000000000000000";
string pixelData = "0000FF0000FF0000FF0000FF0000FF000000FF0000FF0000FF0000FF0000FF000000FF0000FF0000FF0000FF0000FF000000FF0000FF0000FF0000FF0000FF000000FF0000FF0000FF0000FF0000FF00";
string hexImgData = bitmapHeader + bitmapInfoHeader + pixelData;
// Call function to Convert the hex data to byte array
byte[] newByte = ToByteArray(hexImgData);
MemoryStream memStream = new MemoryStream(newByte);
pictureBox1.Image = Bitmap.FromStream(memStream);
It seems you need convert incoming string from Base64 to byte array like this:
byte[] encodedDataAsBytes = System.Convert.FromBase64String(encodedData);