How can i get in c# an image as bitmapimage from an url and pass to it some headers? (not parameters, headers)
And also i found a simple solution but i couldn't understand why it was wrong, the problem is that:
WHen i get byte[] from the server i get the image with a lot of question marks, this appends i thinks because of a different text encoding, how can i fix the code so i can get successfully the image from c# with headers and visible to bitmap?
You have to convert your image into a base64 encoded string and pass it to the headers.
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;
}
}
Then on the other side you can parse it and convert it back to an image:
public Image LoadImage()
{
//data:image/png;base64,
byte[] bytes = Convert.FromBase64String(YOUR_BASE64_ENCODED_STRING);
Image image;
using (MemoryStream ms = new MemoryStream(bytes))
{
image = Image.FromStream(ms);
}
return image;
}
Related
I have and image with a compression type of CCITT T.6 which is converted to base64 and sent to a backend API, where base64 string will be converted back to the original image and validate the file details. My problem is whenI convert the base64 string back to its original image, the compression type for the image has now changed to LZW. Does converting an image to a base64 string change its compression type? If so how can I keep the files original compression type.
string img = "";
using (Image image = Image.FromFile(filepath))
{
using (MemoryStream m = new MemoryStream())
{
image.Save(m, image.RawFormat);
byte[] imageBytes = m.ToArray();
// Convert byte[] to Base64 String
string base64String = Convert.ToBase64String(imageBytes);
img = base64String;
}
}
LoadImage(img);
public void LoadImage(string base64image)
{
byte[] bytes = Convert.FromBase64String(base64image);
Image image;
using (MemoryStream ms = new MemoryStream(bytes))
{
image = Image.FromStream(ms);
}
File.WriteAllBytes(filepath,bytes);
}
Getting rid of the memory stream and using Convert.ToBase64String(File.ReadAllBytes(filepath)) suggested by Steeeve seems to have solved the problem. Image Compression types are now consistent after regenerating the image from a base64 string.
I want to download an image has link like this:
data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAXYAAAAzCAYAAACDvCMOAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAABBmSURBVH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xjuE9na/4vyL7Yf9htYNu7Z3z21qpm6hdZi6Ne3ujsY9t9nBdwS/IbcXdzTuuc0OU7fQOrjnNjWvdwS/IbebO4LfkNvNHY37tLPQOrT7tlto3XiH0N7uN/x+f2B+XubWz+TmAAAAAElFTkSuQmCC
but when I use:
WebClient webClient = new WebClient();
webClient.DownloadFile(remoteFileUrl, localFileName);
it throw to exception: "uri prefix is not recognized"
There is NO image link to download. It is base64 representation of the image, so the image is encoded in the string and embedded into the document.
You can get the base64 representation of the string and convert it to image as done here. Pasting the code for quick reference.
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;
}
}
When i try to convert a base64string to an Image in C#, I'm getting output as "System.Drawing.Bitmap" instead of the actual Image:
public Image DownFile(string base64String)//string file
{
//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("E:/Project Utilitie Connection/FileDownloadTask/Images", System.Drawing.Imaging.ImageFormat.Jpeg);
return image;
}
try a using statement for the MemoryStream, like so:
byte[] bytes = Convert.FromBase64String(base64String);
Image image;
using (MemoryStream ms = new MemoryStream(bytes))
{
image = Image.FromStream(ms);
}
return image;
The framework is converting the Image object to string which is why you are seeing System.Drawig.Bitmap
Create a response, giving it the bytes from the converted base64 string and then setting the content type for response so the client will know how to render the content.
public class ValuesController : ApiController {
[HttpGet]
public IHttpActionResult DownFile(string base64String) {
if (!string.IsNullOrWhiteSpace(base64String)) {
byte[] imageBytes = Convert.FromBase64String(base64String);
var response = Request.CreateResponse(HttpStatusCode.OK);
response.Content = new ByteArrayContent(imageBytes);
response.Content.Headers.ContentType = new MediaTypeHeaderValue("image/jpg");
return ResponseMessage(response);
}
return BadRequest();
}
}
This is a very simplified example to demonstrate how it can be done. Take some time to review and understand what was done so that it can be implemented into your particular scenario.
I've found this sample code to convert an image to base 64, I'm not sure how to pass an image into it though. I want to be able to give a path to a specific directory. I've managed to find a file with this code:
byte[] ImageData = File.ReadAllBytes("storage/emulated/0/DCIM/Camera/img.jpg");
But I need to pass that into the following code.
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;
}
}
Hope you can help. Thanks.
byte[] ImageData = File.ReadAllBytes(path_to_file);
string base64String = Convert.ToBase64String(ImageData);
I'm opening a tiff file from local disk:
Image multiPageImage = Image.FromFile(fileName);
Then sending it to conversion method:
base64string = ImageToBase64(multiPageImage, ImageFormat.Tiff);
public static 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;
}
}
Finally I convert my base64 to tiff file with the below code:
public static void ConvertBase64ToTiff(string base64string)
{
Byte[] bitmapData = new Byte[base64string.Length];
bitmapData = Convert.FromBase64String(FixBase64ForImage(base64string));
using (MemoryStream streamBitmap = new System.IO.MemoryStream(bitmapData))
{
Bitmap bitImage = new Bitmap((Bitmap)Image.FromStream(streamBitmap));
bitImage.Save(#"C:\myTiff.tiff");
}
}
public static string FixBase64ForImage(string base64string)
{
System.Text.StringBuilder sbText = new System.Text.StringBuilder(base64string, base64string.Length);
sbText.Replace("\r\n", String.Empty);
sbText.Replace(" ", String.Empty);
return sbText.ToString();
}
That fixing method is not mine. Actually I don't know if it is the right way or not. But I searched everwhere and trying what I find.
My tests showed that base64 string has just a single image.
Any help would be greatly appreciated!
For people who have a problem about converting multi page tiff file to base64 and vice versa you can refer to that link:
Is it possible to create a base64 string which has all frames of a multi page tiff file?
Maybe it is not totally the same question but the answer will solve this one too.