issue with saved image in a particular location in c# - c#

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..

Related

Download image without prefix of url C#

I want to download an image has link like this:
data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAXYAAAAzCAYAAACDvCMOAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAABBmSURBVHherY5BisRADMTy/0/PngS1gsJusECHlouQ73fM933/BL+hdcOu7d1zm5qpW2gdpm5Nu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Tuuc0OU7fQOrjnNjWvdwS/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;
}
}

How to display image into picture box contain in string

I have an image string contain in an XML file and want to convert the string into an image. I have used the following code which is giving error: Parameter is not valid.
Code is following which is give above error:
Byte[] data = new Byte[0];
byte[] array = Encoding.ASCII.GetBytes("D:\notimg.txt");
MemoryStream mem = new MemoryStream(array);
pictureBox1.InitialImage = null;
pictureBox1.Image = Image.FromStream(mem);
Below is the data contained in XML tag which I need to convert into the image and display into picture box, I have placed the below data into a notepad file and removed the XML tag . Please, anyone, guide how to display the below data into an image. Thanks in advance.
<photograph>/9j/4AAQSkZJRgABAQECWAJYAAD/4QBuRXhpZgAATU0AKgAAAAgAAYdpAAQAAAABAAAAGgAAAAAAAZKGAAcAAAA6AAAALAAAAABVTklDT0RFAABDAHIAZQBhAHQAZQBkACAAYgB5ACAAQQBjAGMAdQBTAG8AZgB0ACAAQwBvAHIAcAAu/9sAQwAIBgYHBgUIBwcHCQkICgwUDQwLCwwZEhMPFB0aHx4dGhwcICQuJyAiLCMcHCg3KSwwMTQ0NB8nOT04MjwuMzQy/9sAQwEJCQkMCwwYDQ0YMiEcITIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIy/8AAEQgBwgFeAwEiAAIRAQMRAf/EAB8AAAEFAQEBAQEBAAAAAAAAAAABAg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W/HFAc5wFA96YBk+n1qTAUcc0gsOyxPapETPXg01flALbgPYU/wA4IRsBB9xmqDYRo2QZ2Fu9K0kQQgR4z69qYJJSSFC/yqMXbq3Qj2IyKWwD12gZ2kD+8vOKcueiPuBpquGOUOxsdu/4U/y2XGR15BFK4xsg5+dcEUi5Qe3oalXdnnml8vPNK4WIjjqBipFbB46U4Rd6csXQY5ouOwh42jtyRTgnzcdKVozwOmKkRe350XsFhwOUIPpTF4OAasqi46HNQSIQSF4+lCkKxLFICQA+D71bjlIPzZ+orMC4PuPep4d+OOcenWqFY6Kx1i4tCmx9yqcgZxiu+0fWIdTh+VgJUHzLXlCSEY7+xFX7DUJbOdZoppRRVDR//2Q==</photograph>
Quick and dirty:
var s = File.ReadAllText(#"d:\file.txt");
s = s.Replace("<photograph>", string.Empty).Replace("</photograph>", string.Empty);
var b = Convert.FromBase64String(s);
using (var mem = new MemoryStream(b))
{
pictureBox1.Image = Image.FromStream(mem);
}
It's Base64
first convert to byte[]
then use
public System.Drawing.Image Base64ToImage(byte[] imageBytes)
{
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;
}
then You Will Got image..!:)

How to convert base64 string to image binary file and save onto server [duplicate]

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

C# - Converting multi page tiff file to base64 string and converting back results with single image

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.

How can I store and retrieve an image using an SQLite database and a WPF application?

I have to store an image from my WPF application to the SQLite database, and then retrieve the same image and display it in the application. I tried doing this by coverting the image into byte array and storing that byte array to the SQLite database as BLOB, but this is not working. Can someone help me?
I would suggest to convert the image to a base64 string first and then store it in the database.
In C#:
Image to Base64 String
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
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;
}
You can save the string in the database. This question is related to it: How do i read a base64 image in WPF?
Why don't you store the path to the image relative to the application's root?

Categories