How to convert Embedded image to Base64 string in xamarin forms..? - xamarin

I want to convert an Embedded image to base 64 string. The image is in PCL solution so let me know how to convert an image into base64. As I tried lots of ways but I am not getting the file path properly. So please help me with this.

//file to base64 string
byte[] b = System.IO.File.ReadAllBytes(FileName);
String s = Convert.ToBase64String(b);
//base64 string to file
byte[] data = Convert.FromBase64String(s);
System.IO.File.WriteAllBytes(FileName2, data);

Since it is an embedded resource getting the path for the image is a problem to convert it into a stream, you can take the following steps to get the path of your embedded image:
string imagePath = "NameOfProject.Assets.applicationIcon.png";
Note: This is the sample path in your case you will give your path, Where the name of the project is the name of the project, Assets is the folder in which I have the image and application icon is the image. (I hope you understood what I am doing here)
After that get the Assembly details something like this:
Assembly assembly = typeof(NameOfClass).GetTypeInfo().Assembly;
Then inside a using statement convert your image into a stream, Something like this
string result;
using (Stream stream = assembly.GetManifestResourceStream(imagePath))
{
long length = stream.Length;
byte[] buffer = new byte[length];
stream.Read(buffer, 0, (int)length);
result = Convert.ToBase64String(data);
}
Revert in case of queries.

Related

Create a 1D barcode and return asd base64 string in .NET6 where it works on all platforms

I need to take a ticket id (only digits, but as a string), turn the string into a 1D barcode, then convert that to a image or bitmap or svg, then convert that into a base64 encoded string, where the base64 string can be returned as a response in a endpoint. It has be done in .NET6 and I'm doing it on a Mac (MacOS)
This is what I have so far (just put it in a console app for testing purposes):
using System.Drawing;
using BarcodeLib;
byte[] ImageToByteArray(Image image)
{
MemoryStream ms = new MemoryStream();
image.Save(ms,image.RawFormat);
return ms.ToArray();
}
var ticketId = "038000356216";
Barcode b = new Barcode();
Image img = b.Encode(TYPE.UPCA, ticketId, Color.Black, Color.White, 290, 120);
var barcodeAsString = Convert.ToBase64String(ImageToByteArray(img));
Console.WriteLine(barcodeAsString);
This does work because the Barcodelib uses Windows specific API's, which then causes a exception on the Mac. This code would properly work on a Windows machine. But since I'm on a Mac this is the the issue that I would like your help with.
How can I do this in a "platform neutral" way - something that will work on both MacOS and Windows?
I'm open for using any open source library/Nuget package that supports this - just as long as I don't have to pay for it, or there is going to be a waterwark on the image.
I have tried several libraries / Nuget packages - all with the same result as in the example.

Convert Base64 String to Image file in Groovy

i checked with old threads but didn't find anything helpful.
i am a JD Edwards developer and we have a requirement in jde Orchestrator to process base64 string.
Can anyone help me and share full code for this?
i am new in groovy script.
def base64str = 'R0lGODlhAwADAHAAACwAAAAAAwADAIHsHCT97KYAAAAAAAACBIQRBwUAOw==' // < base64 string with 3x3 gif inside
def filename = System.properties['user.home']+'/documents/my.gif' // < filename with path
// save decoded base64 bytes into file
new File(filename).bytes = base64str.decodeBase64()
as a result you there should be a new file my.gif in current user/ documents folder
with 3x3 pixels image (43 bytes file)

How do I read/write image file with its original extension when you receive the image as bytes in Flutter?

Here is the problem I have:
My users can set their profile image either from Facebook (which is a jpeg or gif) or from local device (which could be png or jpg or others).
I get the image from Facebook by using:
// Get the name, email and picture
final graphResponse = await http.get(
'https://graph.facebook.com/v4.0/me?fields=name,email,picture.width(300).height(300)&access_token=$token');
// Decode JSON
final profile = jsonDecode(graphResponse.body);
final String stringData = profile['picture']['data'];
final bytes = Uint8List.fromList(stringData.codeUnits);
And getting image from local device by:
final imagePicker = ImagePicker();
// Call image picker
final pickedFile = await imagePicker.getImage(
source: ImageSource.gallery,
maxWidth: MAX_WIDTH_PROFILE_IMAGE,
);
final imageBytes = await pickedFile.readAsBytes();
Then all I got here are in bytes (Uint8List), how do I save it according to its original extension?
Then later on how do I read them again without checking its extension?
Such as with:
// Setting the filename
// Could be jpg or png or bmp or gif.
// How to determine the extension?
final filename = 'myProfileImage';
// Getting App's local directory
final Directory localRootDirectory =
await getApplicationDocumentsDirectory();
final String filePath = p.join(localRootDirectory.path, path, filename);
final file = File(filePath);
You see, when reading the file we need to specify the full filename. But how to determine the extension then ?
You can avoid dealing with extensions completely by simply not setting an extension in the filename. Extensions only exist to indicate what is likely contained within a file for an OS, but they are not necessary and aren't needed in your case especially since you know that you have some kind of image data in that file and you application is probably the only thing ever using that file.
However, if you really do want to use extensions in the filename, you can use the image package. This provides a Decoder abstract class with multiple implementers for a variety of image encoding methods. To determine which method was used for your file you could check with the isValidFile of each possible decoder type that you need and write an extension accordingly.
Example:
PngDecoder png = PngDecoder();
if(png.isValidFile(data //Uint8List inputted here)) {
print("This file is a PNG");
}

Error Converting Apps Script Blob from PNG to JPEG

I'm trying to download a PNG image in Apps Script, convert it to JPEG, and generate a data URI for this new JPEG.
function test() {
var blob = UrlFetchApp.fetch('https://what-if.xkcd.com/imgs/a/156/setup.png').getBlob();
var jpeg = blob.getAs("image/jpeg");
var uri = 'data:image/jpeg;base64,' + Utilities.base64Encode(jpeg.getBytes());
Logger.log(uri);
}
When I run this, I get:
The image you are trying to use is invalid or corrupt.
Even something like:
function test() {
var bytes = UrlFetchApp.fetch('https://what-if.xkcd.com/imgs/a/156/setup.png').getBlob().getBytes();
var jpeg = Utilities.newBlob(bytes, MimeType.PNG).getAs(MimeType.JPEG);
DriveApp.createFile(jpeg);
}
doesn't work.
Your code is correct. This may be a bug, but it's specific to the file you are using, so may as well be a bug in the file (i.e., the file could indeed be corrupted somehow). Or maybe it uses some features of PNG format that Google doesn't handle. Replacing the URL by another one, e.g.,
var blob = UrlFetchApp.fetch('https://cdn.sstatic.net/Sites/mathematica/img/logo#2.png').getBlob();
both functions work as expected.

Convert image from sitecore field to base64 string

I need some help with an issue if anyone could help me. I am trying get an image from a Sitecore field and convert it afterwards to a base64 string.
What i have done is getting that content into an ImageField datatype, but i can't seem to find a solution to convert it to base64.
Sitecore.Data.Fields.ImageField img = itm.Fields["image"];
Is anyone able to help me?
Best regards,
Ionut.
You need to get the media item linked from the image field and create the base64 string from the media item file stream:
ImageField imageField = itm.Fields["Image"];
MediaItem mediaItem = imageField.MediaItem;
Stream stream = mediaItem.GetMediaStream();
Byte[] bytes = new Byte[stream.Length];
stream.Read(bytes, 0, bytes.Length);
img64.Src = "data:" + mediaItem.MimeType + ";base64," + Convert.ToBase64String(bytes);

Resources