C# , convert gif data to binary array to image - c#

this is related with test instrument but need to more help in C# code.
i did more explain here about my question.
i send command to instrument and i just receive data from instrument.
received data is real format (binary) and i just put in to string variable.
here i captured what's inside in string..
http://i.stack.imgur.com/UcYqV.png
then what i want do is i want this string to convert byte array. because my goal is make png file in my pc. instrument manual said this returned data is gif format but returned real type.'
i believe the problem point is when i convert to byte array,,, there are problem...
does anyone has this kind of experiance,.????
/// below is just send command to instrument that i want " Returns an image of the display in .gif format "
my6705B.WriteString("hcop:sdump:data?", true);
string image_format = my6705B.ReadString();
/// what's inside string image_format ??![i attached screenshot png file. this is what i received from instrument. (manual said this is Returns an image of the display in .gif format)][1] ![png file][2]
http://i.stack.imgur.com/UcYqV.png
/// now here i think i did something wrong,
/// the goal is i want change this return data to gif or png or image file.
/// any solution is ok for me
/// i just try that change this data to byte array and then try to change image file.
/// i think some error here because my code success to convert byte array then create image,,, error
//// i believe that i did something wrong in convert byte array.....
System.Text.UnicodeEncoding encode = new System.Text.UnicodeEncoding();
byte[] byte_array22 = encode.GetBytes(image_format);
MemoryStream ms4 = new MemoryStream(byte_array22);
Image image = Image.FromStream(ms4); //// error point
image.Save(#"C:\Users\Administrator\Desktop\imageTest.png");
i believe that my explain is in comment.
let me explain again that my goal is convert gif data to image file.
instrument give us Returns an image of the display in .gif format.
i received this data to string array. < i dont know this is correct or not but for now i put to string array > then i just want to png or jpg file with this gif data.
please advice,.
Joseph Choi

Please Try ImageFormat Like
image.Save(#"C:\Users\Administrator\Desktop\imageTest.png", System.Drawing.Imaging.ImageFormat.Png);
Update:
byte[] byte_array22 = Encoding.Unicode.GetBytes(image_format);
MemoryStream ms4 = new MemoryStream(byte_array22);
Image image = Image.FromStream(ms4, true, true);
image.Save(#"C:\Users\Administrator\Desktop\imageTest.png",System.Drawing.Imaging.ImageFormat.Png);

Hmm ... here are a couple of methods to transform an image to Base64 format (string), and back.
private string ImageToBase64(Image image, System.Drawing.Imaging.ImageFormat format)
{
using (MemoryStream ms = new MemoryStream())
{
// Convert Image to byte[]
image.Save(ms, format);
byte[] imageBytes = ms.ToArray();
// Convert byte[] to Base64 String
string base64String = Convert.ToBase64String(imageBytes);
return base64String;
}
}
private Image Base64ToImage(string base64String)
{
// Convert Base64 String to byte[]
byte[] imageBytes = Convert.FromBase64String(base64String);
using (var ms = new MemoryStream(imageBytes, 0, imageBytes.Length))
{
// Convert byte[] to Image
ms.Write(imageBytes, 0, imageBytes.Length);
Image image = Image.FromStream(ms, true);
return image;
}
}
You can use them and play with them to see if they suit you.
I'm not sure that taking an image and simply dumping it into string array will help you much.

Related

Rotate byte array Image and encode to Base64 string

I am having a very difficult time trying to figure this one out:
I am working with a front-end which
Reads an image taken from a mobile device to a byte array
Converts the byte array to be stored as a Base64 string
Sends the Base64 string to a database
The back-end is only slated to store images as Base64 strings, as it later embeds the image as a Base64 string to an email for the user whose email application will only show Base64-embedded images. The problem is that the images are embedding 90 degrees counter-clockwise in the email, so I am trying to rotate the image 90 degrees before encoding the base64 string which is inserted into a Sql stored procedure (executed from the application). I feel like this code should simply do the trick:
HttpPostedFile img = imageUpload.PostedFile;
Stream stream = img.InputStream;
BinaryReader binaryReader = new BinaryReader(stream);
byte[] bytes = binaryReader.ReadBytes((int)stream.Length);
// Rotate Image Code (help):
using (var memoryStream = new MemoryStream(bytes))
{
var rotateImage = System.Drawing.Image.FromStream(memoryStream); rotateImage.RotateFlip(RotateFlipType.Rotate90FlipNone);
rotateImage.Save(memoryStream, rotateImage.RawFormat);
bytes = memoryStream.ToArray();
}
// End Rotate Image Code
insert.Parameters.AddWithValue("#Image",Convert.ToBase64String(bytes));
But I am receiving an error when I try and run it:
System.ArgumentNullException: Value cannot be null.
Parameter name: encoder at the line "bytes = memoryStream.ToArray();"
When I try and save it as a jpeg the image gets chopped off at the top, but no other formats work.
I think there is a problem in the line:
rotateImage.Save(memoryStream, rotateImage.RawFormat);
Maybe you should specify the format. Try with:
rotateImage.Save(memoryStream, System.Drawing.Imaging.ImageFormat.Jpeg);
Or even better, you can catch and force convertion only when there is an exception:
try
{
rotateImage.Save(memoryStream, rotateImage.RawFormat);
}
catch (System.ArgumentNullException)
{
rotateImage.Save(memoryStream, System.Drawing.Imaging.ImageFormat.Jpeg);
}
I think the issue is related to this answer

C# - Convert JBIG1 images to other image formats

I need to convert a JBIG1 image to another image format, such as JPEG or PNG, but I can't seem to find anything related to this.
This JBIG1 image is received encoded in Base64.
I've tried using System.Drawing in .NET to accomplish this, but a "System.ArgumentException: Parameter is not valid" exception is thrown on calling Image.FromStream() using the JBIG1 byte array data.
See code below:
byte[] binData = ConvertFromBase64StringToArray("BASE64 ENCODED JBIG1 IMAGE GOES HERE");
Image img = binData.ConvertToImage();
img.Save("C:/Images/converted-from-jbig.jpeg", System.Drawing.Imaging.ImageFormat.Jpeg);
Functions used:
public static byte[] ConvertFromBase64StringToArray(string base64String)
{
byte[] data = Convert.FromBase64String(base64String);
using (var stream = new MemoryStream(data, 0, data.Length))
{
data = stream.ToArray();
}
return data;
}
public static Image ConvertToImage(this byte[] byteArrayIn)
{
var ms = new MemoryStream(byteArrayIn);
Image returnImage = Image.FromStream(ms); //exception thrown in this line
return returnImage;
}
Does anyone have any knowledge to share about this topic?
You'll probably need a third party library to work with JBig files. It looks like https://github.com/dlemstra/Magick.NET has support for that.

Converting a QImage to a C# Image

I have a QImage in my C++/Qt application and I would like to store it in a Microsoft SQL Database as a Bytearray. Afterwards, I would like to read it out with a Visual Studio C# Application as a ByteArray and convert it to an Image Object. To simplify :
QImage C++-->Bytearray-->Microsoft SQL-->Bytearray-->ImageC#
Is it possible with png-Pictures or is there an other solution for taking a picture with C++ storing in Microsoft SQL Server and read out with C#?
If I was doing this I would use use base64 as a string to store on the database.
Base64 is often used because it can represent binary data in an ASCII string and is perfect for storing on a database. The reason why base64 is used it that there is only 64 chars in the set and they are all printable characters (no funny weird symbols)
This is how I would go about it:
Step 1
First convert your QImage to a base64 string
QImage image;
image.load("test.png");
QByteArray byteArray;
QBuffer buffer(&byteArray);
image.save(&buffer, "PNG"); // writes the image in PNG format inside the buffer
QString base64Str = QString::fromLatin1(byteArray.toBase64().data())
Step 2
Save this string on the database
Step 3
In C# land, load the base64 string from the database and then convert it to an Image
public Image Base64ToImage(string base64String)
{
// Convert Base64 String to byte[]
byte[] imageBytes = Convert.FromBase64String(base64String);
MemoryStream ms = new MemoryStream(imageBytes, 0, imageBytes.Length);
ms.Write(imageBytes, 0, imageBytes.Length);
Image image = Image.FromStream(ms, true);
return image;
}
Note: The code above has not been tested!

Failing to upload Image in base64 bits via XML-RPC [Wordpress/NGG]

I have literally tried every possible way on this but apparently I just can't figure out the right one.
I am trying to upload images to the next-gen gallery that is installed on a wordpress blog. Everything about xml-rpc is working since I am able to do all other stuff with it.
The problem is that server returns an error saying the image is not a valid one. Which is quite obvious that the problem is about base64. However, for test purposes i copied and checked the base64 string and realized it is just right, it converts right to the image using an external base64 to image converter.
This is the line that submits the query.
result = categories.newImage(1, "admin", "password", newImage);
The struct for newImage is;
public struct imageI
{
public string name;
public string type;
public string bits;
public bool overwrite;
public int gallery;
public int image_id;
}
And this is how a new one is initialized, along with converting an image into base64
//Creating the image base64 string
string filename = "asd.jpg";
Image image1 = Image.FromFile(filename);
string base64 = ImageToBase64(image1, image1.RawFormat);
//for test purposes i copied and checked the base64 and it is just right, it converts right to the image using an external base64 to image converter.
Clipboard.SetText(base64);
//Creating a newImage
imageI newImage = default(imageI);
newImage.name = "newImage";
newImage.bits = base64;
newImage.gallery= 86;
And finally my method "ImageToBase64(Image, ImageFomat)";
public string ImageToBase64(Image image, System.Drawing.Imaging.ImageFormat format)
{
using (MemoryStream ms = new MemoryStream())
{
// Convert Image to byte[]
image.Save(ms, format);
byte[] imageBytes = ms.ToArray();
// Convert byte[] to Base64 String
string base64String = Convert.ToBase64String(imageBytes);
return base64String;
}
}
In case anybody bumps on this, I figured it out, on the line;
newImage.name = "newImage"; the file name should be the same with the one you are uploading, or at least it should have the same extension so that the function in xml-rpc file can resolve the extension and check if it's okay to upload or not.

convert image to byte[] c# and get image back in android

I have the following code in c# which convert image to the byte[]
MemoryStream memory = new MemoryStream();
bitmap.Save(memory, System.Drawing.Imaging.ImageFormat.Jpeg);
now i want to get the image back in the android, what should i do ?
Im not really sure what your problem is as you haven't answered anyones question but if you just want to make a bitmap out of a byte array this how to do it
BitmapFactory.Options opt = new BitmapFactory.Options();
opt.inPreferredConfig = Bitmap.Config.ARGB_8888;
Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length, opt);
Convert ByteArray to Base64 string in C#. Then Convert Base64 string to ByteArray in android an do everything you want. In this method you can work with any object type in addition to bitmap.

Categories

Resources