Hello we are using InkCanvas control on the wpf page. And we are saving Base64String string in the database. See code
private void submitButtonsCommand_Event(object sender)
{
byte[] sigByte;
InkCanvas icSignature = sender as InkCanvas;
using (var memoryStream = new MemoryStream())
{
icSignature.Strokes.Save(memoryStream);
sigByte = memoryStream.ToArray();
}
ServiceCallReportSignatureModel.SigCustomerSignature = Convert.ToBase64String(sigByte);
UpdateRecord();
}
And when we are retrieving data from the database and converting into the FromBase64String and after that when we creating image then we are getting the error.
public Image LoadImage(base64string)
{
//data:image/gif;base64,
//this image is a single pixel (black)
byte[] bytes = Convert.FromBase64String(base64string);
Image image;
using (MemoryStream ms = new MemoryStream(bytes))
{
image = Image.FromStream(ms);
}
return image;
}
I don't know why this is occurring. Please help me find out the error.
Our base64 string is : "AIwBAwxIEEWfARsCAAb/RjURAACAPx8JEQAAAAAAAPA/Cmwsh/EgR4kP+Etw/ojDWG7NZ8OZQxBN5otEzmU1mk04I7DnATcLHomzzUbxw/h3Dtp16yhlTQ2UsoTQh+uE64XgjsN6Js2UZpAZmFpmaZTKzzKZ4ctFmw3Mlmm0Bw9h/DtpTPKu79CYgm+VJqA="
Actually we are taking signature from the ink canvas control and we want to display in the pdf when we need. So we want create image from the base 64 data but we are getting the error.
The StrokeCollection.Save(Stream) method does not save a bitmap. Instead,
The Save method saves the StrokeCollection as Ink Serialized Format (ISF).
You can restore the saved StrokeCollection by passing a Stream with the saved data to the StrokeCollection(Stream) constructor.
Related
I want to upgrade from iTextSharp to iText7.
I cant set the image of PdfButtonField to an System.Drawing.Image in the past.
But now in iText7, the PdfButtonFormField.SetImage(string sImagePah) can only acccept file path.
I create an System.Drawing.Image the fly, How can I set the image the an AcroField in iText7.NET ?
You can use SetValue method and provide Base64-encoded image value there.
Example code to covert System.Drawing.Image to a Base64 string:
public static string ImageToBase64(System.Drawing.Image image)
{
using (MemoryStream ms = new MemoryStream())
{
image.Save(ms, ImageFormat.Png);
byte[] imageBytes = ms.ToArray();
return Convert.ToBase64String(imageBytes);
}
}
Example code to set the image to the form field:
PdfButtonFormField field = ...;
field.SetValue(ImageToBase64(image));
I need to convert a JBIG1 image to another image format, such as JPEG or PNG, but I can't seem to find anything related to this.
This JBIG1 image is received encoded in Base64.
I've tried using System.Drawing in .NET to accomplish this, but a "System.ArgumentException: Parameter is not valid" exception is thrown on calling Image.FromStream() using the JBIG1 byte array data.
See code below:
byte[] binData = ConvertFromBase64StringToArray("BASE64 ENCODED JBIG1 IMAGE GOES HERE");
Image img = binData.ConvertToImage();
img.Save("C:/Images/converted-from-jbig.jpeg", System.Drawing.Imaging.ImageFormat.Jpeg);
Functions used:
public static byte[] ConvertFromBase64StringToArray(string base64String)
{
byte[] data = Convert.FromBase64String(base64String);
using (var stream = new MemoryStream(data, 0, data.Length))
{
data = stream.ToArray();
}
return data;
}
public static Image ConvertToImage(this byte[] byteArrayIn)
{
var ms = new MemoryStream(byteArrayIn);
Image returnImage = Image.FromStream(ms); //exception thrown in this line
return returnImage;
}
Does anyone have any knowledge to share about this topic?
You'll probably need a third party library to work with JBig files. It looks like https://github.com/dlemstra/Magick.NET has support for that.
I have an image in my images folder
and I want to pass this image to an Image object so I can then convert it to a base 64 string. This will then be passed to the client and inserted into the src of a img element.
The problem is that I don't know how to get the image from disk into the image object.
Image img = Image.FromFile(#"..Images\no_image.jpg"); // doesn't work
string image = Convert.ToBase64String(imageToByteArray(img));
public byte[] imageToByteArray(Image imageIn)
{
MemoryStream ms = new MemoryStream();
imageIn.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
return ms.ToArray();
}
If you work on a web project, you should use:
Image.FromFile(Server.MapPath("~/Images/no_image.jpg"))
else if it is a WinForm/WPF/Console application, use:
Image.FromFile(System.IO.Path.Combine(Environment.CurrentDirectory, "Images", "no_image.jpg"))
I am trying to save image date to physical file
Below is image data which I got from a jpeg image (via some browser response):
data:image/jpeg;base64,/9j/4AAQSkZJRgABAgEASABIAAD/.....blah blah .....//2Q==
Below is the code I am using to save image data string to Image
Image image = LoadImage(dataURL);
image.Save(saveLocation);
image.Dispose();
public Image LoadImage(string imageString)
{
imageString = imageString.Substring(imageString.IndexOf(',') + 1);
byte[] bytes = Convert.FromBase64String(imageString);
Image image;
using (MemoryStream ms = new MemoryStream(bytes))
{
image = Image.FromStream(ms);
}
return image;
}
Getting the following exception at image.Save(saveLocation); line
A generic error occurred in GDI+.
I have no issues when the image source with png images with the same code but with jpeg images no matter what the size of jpeg is I am getting that exception every time.
I am able to save the png images with same code at same location.
Edit: Please note that I am getting just data image string from browser (which I am capturing via clipboard of browser) i.e I am not getting bytes from client.
Is there any limit for the bytes which can be converted to image stream and then save back to physical file?
Is there any other approach to do the same?
There doesn't seem to be any reason to go through Image at all here.
Assuming the posted data is valid, just write the content to a file directly:
SaveImage(dataURL, saveLocation);
public bool SaveImage(string imageString, string location)
{
try {
imageString = imageString.Substring(imageString.IndexOf(',') + 1);
byte[] bytes = Convert.FromBase64String(imageString);
using (FileStream fs = new FileStream(location, FileMode.Create))
{
fs.Write(bytes, 0, bytes.Count);
}
}
catch(Exception)
{
return false;
}
return true;
}
Is there a way in C# to do this conversion and back?
I have a WPF app which has a Image control. I'm trying to save the image in that control to a SQL Database.
In my Entity Model, the datatype of the picture column in my database is a byte[]. So I found a method to convert a System.Drawing.Image to a byte[] and back. But I haven't found a method to convert from System.Windows.Controls.Image to a byte[].
So that's why I now need to do the above conversion.
If you have a byte array that represents a file that WPF can decode (bmp, jpg, gif, png, tif, ico), you can do the following:
BitmapSource LoadImage(Byte[] imageData)
{
using (MemoryStream ms = new MemoryStream(imageData))
{
var decoder = BitmapDecoder.Create(ms,
BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.OnLoad);
return decoder.Frames[0];
}
}
Likewise, to convert it back, you can do the following:
byte[] SaveImage(BitmapSource bitmap)
{
using (MemoryStream ms = new MemoryStream())
{
var encoder = new BmpBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(bitmap));
encoder.Save(ms);
return ms.GetBuffer();
}
}
Well, one is an image and one is a control that shows an image, so I don't think that there's a conversion between the two. But, you could set the Source of the ...Controls.Image to be your ...Drawing.Image.
Edit based on update
Does this do what you need - http://msdn.microsoft.com/en-us/library/ms233764%28VS.100%29.aspx