save a image from base64 - c#

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.

Related

Convert Base64 String to Bitmap or Image Xamarin or Android [duplicate]

This question already has answers here:
C# Base64 String to JPEG Image
(4 answers)
Closed 6 years ago.
I'm trying to convert a Base64 string to an image and set ImageView using the same image. I know how to get it done in java, but I'm having a trouble in C#. Anyone have an idea of how to get it done in C#?
Some of the code I've tried;
public Image Base64ToImage(string base64String)
{
// Convert base 64 string to byte[]
byte[] imageBytes = Convert.FromBase64String(base64String);
// Convert byte[] to Image
using (var ms = new MemoryStream(imageBytes, 0, imageBytes.Length))
{
Image image = Image.FromStream(ms, true);
return image;
}
}
Base64 to Bitmap :
public Bitmap Base64ToBitmap(String base64String)
{
byte[] imageAsBytes = Base64.Decode(base64String, Base64Flags.Default);
return BitmapFactory.DecodeByteArray(imageAsBytes, 0, imageAsBytes.Length);
}
Bitmap to Base64 :
public String BitmapToBase64(Bitmap bitmap)
{
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
bitmap.Compress(Bitmap.CompressFormat.Png, 100, byteArrayOutputStream);
byte[] byteArray = byteArrayOutputStream.ToByteArray();
return Base64.EncodeToString(byteArray, Base64Flags.Default);
}
Image to Base64 String,
online conversion - here
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;
}
}
Base64 String to Image,
online conversion - here
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;
}
I'm assuming that your encoded data is some bitmap format? If so, you should be able to do something like this:
public Bitmap Base64ToImage(string base64String)
{
// Convert base 64 string to byte[]
byte[] imageBytes = Convert.FromBase64String(base64String);
// Convert byte[] to Image
using (var ms = new MemoryStream(imageBytes, 0, imageBytes.Length))
{
Bitmap image = BitmapFactory.FromStream(ms, true);
return image;
}
}

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

convert base64 string into image

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");

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);
}

How to Convert image (.png) to base64 string, vice a versa and strore it to a specified location

I'm trying to store images (png) to sqlite database in a windows 8 app, and i figured out it can be done by converting it to base64 string and storing the string to the database. Later on in the app i want to convert that base64 string to png image and store it to a specified location. The Problem is i don't know how to convert images to base64 and base64 to image and store it to a specified location in c# windows 8 app. Any help would be appreciated.
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
return Convert.ToBase64String(imageBytes);
}
}
public 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);
return Image.FromStream(ms, true);
}
}

Categories

Resources