I want to download an image has link like this:
data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAXYAAAAzCAYAAACDvCMOAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAABBmSURBVHherY5BisRADMTy/0/PngS1gsJusECHlouQ73fM933/BL+hdcOu7d1zm5qpW2gdpm5Nu7ujcc9tdvAdwW/I7cUdjXtus8PULbQO7rlNzesdwW/I7eaO4DfkdnNH4z7tLLQO7b7tFlo33iG0t/sV519sP+w3tG7Ytb17blMzdQutw9StaXd3NO65zQ6+I/gNub24o3HPbXaYuoXWwT23qXm9I/gNud3cEfyG3G7uaNynnYXWod233ULrxjuE9na/4vyL7Yf9htYNu7Z3z21qpm6hdZi6Ne3ujsY9t9nBdwS/IbcXdzTuuc0OU7fQOrjnNjWvdwS/IbebO4LfkNvNHY37tLPQOrT7tlto3XiH0N7uV5x/sf2w39C6Ydf27rlNzdQttA5Tt6bd3dG45zY7+I7gN+T24o7GPbfZYeoWWgf33Kbm9Y7gN+R2c0fwG3K7uaNxn3YWWod233YLrRvvENrb/YrzL7Yf9htaN+za3j23qZm6hdZh6ta0uzsa99xmB98R/IbcXtzRuOc2O0zdQuvgntvUvN4R/Ibcbu4IfkNuN3c07tPOQuvQ7ttuoXXjHUJ7u19x/sX2w35D64Zd27vnNjVTt9A6TN2adndH457b7OA7gt+Q24s7GvfcZoepW2gd3HObmtc7gt+Q280dwW/I7eaOxn3aWWgd2n3bLbRuvENob/crzr/YfthvaN2wa3v33KZm6hZah6lb0+7uaNxzmx18R/AbcntxR+Oe2+wwdQutg3tuU/N6R/Abcru5I/gNud3c0bhPOwutQ7tvu4XWjXcI7e1+xfkX2w/7Da0bdm3vntvUTN1C6zB1a9rdHY17brOD7wh+Q24v7mjcc5sdpm6hdXDPbWpe7wh+Q243dwS/IbebOxr3aWehdWj3bbfQuvEOob3drzj/Yvthv6F1w67t3XObmqlbaB2mbk27u6Nxz2128B3Bb8jtxR2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O7rlNzesdwW/I7eaO4DfkdnNH4z7tLLQO7b7tFlo33iG0t/sV519sP+w3tG7Ytb17blMzdQutw9StaXd3NO65zQ6+I/gNub24o3HPbXaYuoXWwT23qXm9I/gNud3cEfyG3G7uaNynnYXWod233ULrxjuE9na/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;
}
}
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.
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;
}
This question already has answers here:
C# Base64 String to JPEG Image
(4 answers)
Closed 7 years ago.
As an example I have converted a canvas element with a re-sized image and posted into a hidden input field that's now encoded as
data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD...
This value then posted to the same page which I need to convert this string into an image and save onto the server.
Code Behind File (upload.aspx)
protected void btnUpload_Click(object sender, EventArgs e)
{
HttpPostedFile filePosted = Request.Files["newinput"];
string base64String = filePosted.ToString();
// 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);
System.Drawing.Image image = System.Drawing.Image.FromStream(ms, true);
//I DONT KNOW HOW TO WRITE ABOVE INTO THE SaveAs CONDITION BELOW
if (filePosted != null && filePosted.ContentLength > 0)
{
string fileNameApplication = Path.GetFileName(filePosted.FileName);
string fileExtensionApplication = Path.GetExtension(fileNameApplication);
// generating a random guid for a new file at server for the uploaded file
string newFile = Guid.NewGuid().ToString() + fileExtensionApplication;
// getting a valid server path to save
string filePath = Path.Combine(Server.MapPath("~/Assets/") + Request.QueryString["id"] + "/", newFile);
if (fileNameApplication != String.Empty)
{
filePosted.SaveAs(filePath);
}
}
I'm pretty sure I need to convert this the imagedata to a binary file before saving on the server but I can't quite get how I need to amend the code above. Any ideas? The code to save to the server doesn't work.
Once I have converted this to an image and changed it's name as above - I'm storing this back to a database via LINQ - with a URL appended to it.
Any help would be greatly appreciated.
Hope below functions helps.
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 1 -
From the comments it seems that you are getting base64 string and you need to save it as image on server and then whenever required you need to show that image using physical server path.
Ok. Base64ToImage will give you image for your base64 string. You can save it on server using
image.Save("PATH", System.Drawing.Imaging.ImageFormat.Jpeg);
And this "PATH" you have supplied or created can be stored in DB as URL, which you can use at the time of display.
Note: Make sure that you have write access to folder where you are saving image.
EDIT-2
Your function should look like below. Please put validation code, error handling as required.
protected void btnUpload_Click(object sender, EventArgs e)
{
HttpPostedFile filePosted = Request.Files["newinput"];
string base64String = filePosted.ToString();
// 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);
System.Drawing.Image image = System.Drawing.Image.FromStream(ms, true);
string newFile = Guid.NewGuid().ToString() + fileExtensionApplication;
string filePath = Path.Combine(Server.MapPath("~/Assets/") + Request.QueryString["id"] + "/", newFile);
image.Save(filepath,ImageFormat.Jpeg);
}
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.
I am able to convert image to base64 string and base64 string to image . And I am saving the image to a particular location. If I run for the first time, I am getting the correct image file which I gave as input. but from the second run, whatever may be the input image I pass, I am getting only the first input image.
Desired output :
I need to get back the input image that I passed.
conversion code :
public string ImageToBase64(Image image, 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;
}
save file code :
public void retreiveAndSaveImgFile(string base64formofstr)
{
Console.WriteLine("the incoming string :: " + base64formofstr.Length);
/* Bitmap bitmap = new Bitmap(Base64ToImage(base64formofstr));
Bitmap newBitmap = new Bitmap(bitmap);
newBitmap.SetResolution(150, 150); // resolution of the original image for which the zone template is created
newBitmap.Save("D:\\Suraya\\TesttogetCorrectPutput\\MICR_SAMPLE.tif", ImageFormat.Tiff);*/
Base64ToImage(base64formofstr).Save("D:\\Suraya\\TesttogetCorrectPutput\\MICR_SAMPLE.tif");
}
Please help me in resolving this problem.
I am not that smart with all of this but till I understand the problem you have is because of cache... As you are using the same name all time, it shows the output from the cache instead of conversion..
First Alternate is to add a time stamp to the name of image..
Second Alternate is to force the browser to reload the complete page instead of showing it from the cache..