convert base64 string into image - c#

I m trying to convert base64 string into image using below code
byte[] imageBytes = Convert.FromBase64String(base64String);
MemoryStream ms = new MemoryStream(imageBytes, 0, imageBytes.Length);
// Convert byte[] to Image
ms.Write(imageBytes, 0, imageBytes.Length);
Image image = Image.FromStream(ms, true);
but it is always giving me an error "parameter is not valid."
And also I want to store image in folder after conversion.

You do not Need
ms.Write(imageBytes, 0, imageBytes.Length);
and for your stream you can use:
MemoryStream ms = new MemoryStream(imageBytes)
thats all i think.
Remember to use using blocks for your MemoryStream
Image image;
using (var ms= new MemoryStream(Convert.FromBase64String(base64String)))
{
image = Image.FromStream(ms, true);
}
To store your Image in FileSytem just use:
image.Save("c:\\myimage.bmp");

Related

How to Convert Base64 String into Png Image Mvc5 C#?

I am Trying to Convert a Base64 encoded string to A Png Image, But the Code Shows Exception of Parameter is Not Valid on Image.FromStream(). After Debugging i cam up with this error on MemoryStream Object "ReadTimeout = 'ms.ReadTimeout' threw an exception of type 'System.InvalidOperationException'" . I am stuck, Is there any Solution or Alternative to Convert String to Png in C#?
Here is My code
string code = "string";
var databytes = Encoding.Unicode.GetBytes(code);
var base64 = Convert.ToBase64String(databytes);
Byte[] Bytes = Convert.FromBase64String(base64);
//Stream bytes
MemoryStream ms = new MemoryStream(Bytes, 0, Bytes.Length);
//convert image
Image newImage = Image.FromStream(ms);
newImage.Save("~/Content/");
Try this:
byte[] bytes = Convert.FromBase64String(base64);
Image image;
using (MemoryStream ms = new MemoryStream(bytes))
{
image = Image.FromStream(ms);
}
You can convert Base64 string into png in this way:
byte[] bytes = Convert.FromBase64String(base64);
Image image;
using (MemoryStream ms = new MemoryStream(bytes))
{
image = Image.FromStream(ms);
image.Save("~/Content/", System.Drawing.Imaging.ImageFormat.Png);
}

how to bind document which contains image to the json object in the controller

I am binding object to the json object.I bind all other properties successfully except document which contains image.
here is my code
json.Append(string.Format("\"Name\":\"{0} {1}\",", rp.Fname, rp.Lname));
json.Append(string.Format("\"City\":\"{0}\",", rp.City));
json.Append(string.Format("\"State\":\"{0}\",", rp.State));
json.Append(string.Format("\"Country\":\"{0}\",", rp.Country));
json.Append(string.Format("\"Document\":\"{0} {1}\",", rp.Document));
in my Document object Content, ContentType,Name,FileSize ,Date,StoragePath
but on view page i got
Uncaught SyntaxError: Unexpected string error
convert the image to a base 64 string and then append
using (Image image = Image.FromFile(Path))
{
using (MemoryStream m = new MemoryStream())
{
image.Save(m, image.RawFormat);
byte[] imageBytes = m.ToArray();
// Convert byte[] to Base64 String
string base64String = Convert.ToBase64String(imageBytes);
return base64String;
}
}
To Convert a string back to Image use :
byte[] imageBytes = Convert.FromBase64String(base64String);
MemoryStream ms = new MemoryStream(imageBytes, 0, imageBytes.Length);
ms.Write(imageBytes, 0, imageBytes.Length);
System.Drawing.Image image = System.Drawing.Image.FromStream(ms, true);
return image;
Also Please Check this URL for more info :
http://www.c-sharpcorner.com/blogs/convert-an-image-to-base64-string-and-base64-string-to-image

save a image from base64

I am using the below code to save image in asp.net with C#. There is no error while saving but I am unable to specify the path in the function.
public Image Base64ToImage(string base64String)
{
// Convert Base64 String to byte[]
byte[] imageBytes = Convert.FromBase64String(base64String);
MemoryStream ms = new MemoryStream(imageBytes, 0,
imageBytes.Length);
// Convert byte[] to Image
ms.Write(imageBytes, 0, imageBytes.Length);
Image image = Image.FromStream(ms, true);
image.Save("test", ImageFormat.Png);
//image.Save("test.png");
return image;
}
If I put the physical path using server.mappath it producing an error GDI exception. What I am doing wrong.

I can' t use Image to Base64 String function

I'm making multi client chat server(done).
but I want to add image send.
I used this method when sending a message
client1 write message and getbyte -> Server -> getstring
So I'm coding like this (I can't this part)
load image -> ImageToBase64(image) -> getbyte --> server --> getstring-> Base64ToImage
Client Send part
how can i use this function and this method can be run.
private void button1_Click(object sender, EventArgs e)
{
Image screen;
screen=Image.FromFile("C:\\Users\\User\\Desktop\\Paint.bmp");
screentext = ImageToBase64(screen); //actually I do not know what parameter should be here
StreamWriter wrthr = new StreamWriter(#"C:\Users\User\Desktop\giden.txt");
wrthr.Write(screentext);
wrthr.Close(); // result empty txt
}
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;
}
}
public Image Base64ToImage(string base64String)
{
// Convert Base64 String to byte[]
byte[] imageBytes = Convert.FromBase64String(base64String);
MemoryStream 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;
}
EDIT: There are two problems here...
Picking a second argument to ImageToBase64
It's up to you - do you want to keep it as a BMP file? If so, you don't really need to go via an Image at all... just load the bytes from Paint.bmp, and convert them directly into Base64:
byte[] bytes = File.ReadAllBytes(#"C:\Users\User\Desktop\Paint.bmp");
File.WriteAllText(#"C:\Users\User\Desktop\giden.txt",
Convert.ToBase64String(bytes));
Or you may wish to choose a different format, e.g. PNG or JPEG, e.g.
string screentext = ImageToBase64(screen, ImageFormat.Png);
(It's not clear where screentext is declared at the moment, but it should almost certainly be a local variable.)
Converting from base64...
This is the problem:
MemoryStream ms = new MemoryStream(imageBytes, 0,
imageBytes.Length);
// Convert byte[] to Image
ms.Write(imageBytes, 0, imageBytes.Length);
You don't need the Write call, and it's actively harmful here:
You've constructed the MemoryStream with imageBytes, so it already contains that data
By writing to the stream, you've moved the "cursor" to the end... so there's nothing to read
Just use:
public Image Base64ToImage(string base64String)
{
// Convert Base64 String to byte[]
byte[] imageBytes = Convert.FromBase64String(base64String);
MemoryStream ms = new MemoryStream(imageBytes);
return Image.FromStream(ms, true);
}

Convert base64 string to image in C# on Windows Phone

I have a base64 string and I want convert that to an image and set the Source of an Image control to the result of that.
Normally I would do that using Image.FromStream, similar to this:
Image img;
byte[] fileBytes = Convert.FromBase64String(imageString);
using(MemoryStream ms = new MemoryStream())
{
ms.Write(fileBytes, 0, fileBytes.Length);
img = Image.FromStream(ms);
}
However, the Image.FromStream method does not exist on Windows Phone, and a casual search only turns up results that depend on that method.
You can use a method like this:
public static BitmapImage base64image(string base64string)
{
byte[] fileBytes = Convert.FromBase64String(base64string);
using (MemoryStream ms = new MemoryStream(fileBytes, 0, fileBytes.Length))
{
ms.Write(fileBytes, 0, fileBytes.Length);
BitmapImage bitmapImage = new BitmapImage();
bitmapImage.SetSource(ms);
return bitmapImage;
}
}
Add an image to your XAML, such as this:
<Image x:Name="myWonderfulImage" />
You can then set the source, like this:
myWonderfulImage.Source = base64image(yourBase64string);

Categories

Resources