I'm trying to save to clipboard an image in base64. But i can't find methods to do it. I can get the base64 string and save it, but not an Image :
public void GetUrl(ExportEventArgs Args)
{
string dataURL = Args.DataUrl;
//byte[] bytes = Convert.FromBase64String(dataURL);
//System.Drawing.Image image;
//using (MemoryStream ms = new MemoryStream(bytes))
//{
// image = System.Drawing.Image.FromStream(ms);
//}
Clipboard.SetTextAsync(dataURL);
}
I'm using .NET MAUI Blazor and the Clipboard class is Microsoft.Maui.ApplicationModel.DataTransfer.Clipboard but i can't find how do it.
Thanks :)
Related
//Retrieve an image in Jpg format and store it into a variable.
var imgFile = (ImageFile)ScanerItem.Transfer(FormatID.wiaFormatJPEG);
var Path = #"D:\ScanImg.jpg";
// save the image in some path with filename.
imgFile.SaveFile(Path);
pictureBox1.ImageLocation = Path;
Based on the following SO answer, you can get the byte[] of the image with (byte[])imgFile.FileData.get_BinaryData():
converting .__comobj WIA to byte[] in C# application
A bitmap can be put as source into the PictureBox.
Therefore the byte[] as to put into a MemoryStream and create a Bitmap:
var imgFile = (ImageFile)ScanerItem.Transfer(FormatID.wiaFormatJPEG);
byte[] imageBytes = (byte[])imgFile.FileData.get_BinaryData();
Bitmap image;
using (MemoryStream stream = new MemoryStream(imageBytes))
{
image = new Bitmap(stream);
}
pictureBox1.Image = image;
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/4vyL7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XWYujXt7o7GPbfZwXcEvyG3F3c07rnNDlO30Dq45zY1r3cEvyG3mzuC35DbzR2N+7Sz0Dq0+7ZbaN14h9De7lecf7H9sN/QumHX9u65Tc3ULbQOU7em3d3RuOc2O/iO4Dfk9uKOxj232WHqFloH99ym5vWO4DfkdnNH8Btyu7mjcZ92FlqHdt92C60b7xDa2/2K8y+2H/YbWjfs2t49t6mZuoXWYerWtLs7GvfcZgffEfyG3F7c0bjnNjtM3ULr4J7b1LzeEfyG3G7uCH5Dbjd3NO7TzkLr0O7bbqF14x1Ce7tfcf7F9sN+Q+uGXdu75zY1U7fQOkzdmnZ3R+Oe2+zgO4LfkNuLOxr33GaHqVtoHdxzm5rXO4LfkNvNHcFvyO3mjsZ92lloHdp92y20brxDaG/3K86/2H7Yb2jdsGt799ymZuoWWoepW9Pu7mjcc5sdfEfwG3J7cUfjntvsMHULrYN7blPzekfwG3K7uSP4Dbnd3NG4TzsLrUO7b7uF1o13CO3tfsX5F9sP+w2tG3Zt757b1EzdQuswdWva3R2Ne26zg+8IfkNuL+5o3HObHaZuoXVwz21qXu8IfkNuN3cEvyG3mzsa92lnoXVo92230LrxDqG93a84/2L7Yb+hdcOu7d1zm5qpW2gdpm5Nu7ujcc9tdvAdwW/I7cUdjXtus8PULbQO7rlNzesdwW/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;
}
}
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;
}
My aim is to convert a base64 string into an image and save it to the disk. I have the following code (mostly from this SO answer) -
namespace ImageSaver
{
using System;
static class Program
{
public static void LoadImage()
{
//get a temp image from bytes, instead of loading from disk
//data:image/gif;base64,
//this image is a single pixel (black)
byte[] bytes = Convert.FromBase64String("R0lGODlhAQABAIAAAAAAAAAAACH5BAAAAAAALAAAAAABAAEAAAICTAEAOw==");
Image image;
Bitmap bimage;
using (MemoryStream ms = new MemoryStream(bytes))
{
image = Image.FromStream(ms);
}
bimage = new Bitmap(image);
bimage.Save("c:\\img.gif", System.Drawing.Imaging.ImageFormat.Gif);
}
static void Main(string[] args)
{
ImageSaver.Program.LoadImage();
}
}
}
I ran it on MS Visual Studio 2010 Ultimate (Windows 8.1 Pro x64), without errors, but I could not find the image file in C drive. Where did I go wrong?
Try to save your file in this way:
File.WriteAllBytes(#"c:\img.gif", bytes);
EDIT
Anyway put your code under try'n'catch to better understand why you file is not being saved.
Try updating LoadImage to this to find out what the error is, and set a debug point on the MessageBox to examine the full details of what's happening;
public static void LoadImage()
{
try
{
//get a temp image from bytes, instead of loading from disk
//data:image/gif;base64,
//this image is a single pixel (black)
byte[] bytes = Convert.FromBase64String("R0lGODlhAQABAIAAAAAAAAAAACH5BAAAAAAALAAAAAABAAEAAAICTAEAOw==");
Image image;
Bitmap bimage;
using (MemoryStream ms = new MemoryStream(bytes))
{
image = Image.FromStream(ms);
}
bimage = new Bitmap(image);
bimage.Save("c:\\img.gif", System.Drawing.Imaging.ImageFormat.Gif);
}
catch (Exception ex) {
MessageBox.Show(ex.Message);
}
}
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.