Convert.FromBase64String usage in VS22 causing output dll to be quarantined by CrowdStrike - c#

I have a C# project that uses Convert.FromBase64String(string) that builds properly in VS2019 but in VS22, the output dll gets immediately quarantined by CrowdStrike. I can't explain why CrowdStrike only finds potential security issues with VS22 and not VS2019.
Is there a way to create an image from Base64 without the use of FromBase64String?
I've found leaving the data:image/png;base64 header in the string allows the project to build and output the dll but Convert.FromBase64String(string) fails with 'The input is not a valid Base-64 string as it contains a non-base 64 character...'
In trying to find a workaround for FromBase64String(), I tried converting the Base64 string to a Byte[] with GetBytes(), formatting for proper length, converting to char[], then ToBase64CharArray, and finally back to my imageBytes with Convert.FromBase64CharArray. This builds and creates my dll but fails with 'invalid parameter' in Image.FromStream(memoryStream)
public static Image Base64ToImage(string base64String)
{
// Remove the data:image/png;base64
base64String = base64String.Substring(base64String.IndexOf(',') + 1);
base64String = base64String.Trim('\0');
// Convert Base64 String to byte[]
byte[] imageBytes = Convert.FromBase64String(base64String);
using (MemoryStream memoryStream = new MemoryStream(imageBytes, 0, imageBytes.Length))
{
// Convert byte[] to Image
if (imageBytes.Length > 0)
{
using (var image = ((Bitmap)Image.FromStream(memoryStream)).RemoveAlpha())
{
return new Bitmap(image);
}
}
else
return null;
}
}

Related

Loading base64 string image to DocuVieware

Image files are saved in base64 string format in the database.
I'm trying to load the image using DocuVieware via MemoryStream but failed.
Does anyone tried this kind of approach?
if (this.IsPostBack != true)
{
var file = new DataFileManager().GetImageByID(userID);
byte[] byt = System.Text.Encoding.UTF8.GetBytes(file.CurrentImage);
DocuVieware1.LoadFromStream(new MemoryStream(byt, 0, byt.Length), true);
}
You probably need to replace:
byte[] byt = System.Text.Encoding.UTF8.GetBytes(file.CurrentImage);
by:
byte[] data = Convert.FromBase64String(file.CurrentImage);
Ref: https://learn.microsoft.com/en-us/dotnet/api/system.convert.frombase64string?view=netframework-4.8

Converting from Image to Base64 returns Error

I am trying to convert an image into a base64 String so i can save in a column in my database. Now i see this method
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;
}
}
And it gives me an Error in ASP.NET saying it does not contain the definition for Save , asks if i am missing an Assembly reference. And when i call it in the main method like this
string base64ImageString = ImageToBase64(img, System.Drawing.Imaging.ImageFormat.Jpeg);
of which the main code looks like this now :
sigObj.SetImageFileFormat(0);
sigObj.SetImageXSize(500);
sigObj.SetImageYSize(150);
sigObj.SetImagePenWidth(8);
sigObj.SetJustifyX(5);
sigObj.SetJustifyY(5);
sigObj.SetJustifyMode(5);
System.Drawing.Image img = sigObj.GetSigImage();
base64ImageString = ImageToBase64(img, System.Drawing.Imaging.ImageFormat.Jpeg);
It gives me another funny error there which looks like this
Cannot convert from System.Drawing.Image to System.Web.UI.Web.Controls.Image
Please what exactly am I doing wrong. I am doing this on ASP.NET webform
The declaration of your method is getting clashed with System.Drawing.Image and System.Web.UI.Controls. Use it like
public string ImageToBase64(System.Drawing.Image image,System.Drawing.Imaging.ImageFormat format)
The last error tells you exactly what the issue is.
In the code file you have a reference to System.Web.UI.Web.Controls.Image, so in the method signature your reference to Image image is actually referring to that package.
You can test this by hovering over the reference and you should see the full path as above.
The reference that you actually need (and that contains the Save() method that you're trying to use is System.Drawing.Image.

get screenshot image data plain [duplicate]

I would need to take a screenshot, if easy to without saving it. I would sent the image data directly to a PHP script. Because I don't have this PHP script at the moment, so I search for the easiest way of format in which I should convert the screenshot data. For debugging reasons until I've got my PHP script I would like to convert a picture of these data on my client side using C#.
My code at the moment for taking a screenshot and convert it (I'm not sure if I can convert the output in my logfile back into a picture):
internal static byte[] ImageToByteArray(Image img)
{
byte[] byteArray = new byte[0];
MemoryStream stream = new MemoryStream();
img.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
stream.Close();
byteArray = stream.ToArray();
return byteArray;
}
public static string TakeScreenshot()
{
String filepath = #"C:\log2.txt";
Bitmap bitmap = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height, PixelFormat.Format32bppArgb);
Graphics graphics = Graphics.FromImage(bitmap);
graphics.CopyFromScreen(Screen.PrimaryScreen.Bounds.X, Screen.PrimaryScreen.Bounds.Y, 0, 0, Screen.PrimaryScreen.Bounds.Size, CopyPixelOperation.SourceCopy);
graphics.Dispose();
bitmap.Save("C:\\temp.png");
Image img = (Image)bitmap;
string str = System.Text.Encoding.Default.GetString(ImageToByteArray(img));
System.IO.File.AppendAllText(filepath, str);
//just for debugging
return "OH";
}
Main question at the moment, is there any way to get a picture back from my converted code (log2.txt).
This line of code:
string str = System.Text.Encoding.Default.GetString(ImageToByteArray(img));
Will almost certainly not do what you want it to do. It will try to interpret your byte array as a string in whatever is the default character encoding on your machine. If the default is something like UTF-8 or any multibyte character set, then it's quite likely to fail. Even if the default encoding is a single-byte character set, it could create a string that can't be reliably turned back into the original byte array.
If you really want to store the byte array as text, you can call Convert.ToBase64String:
string str = Convert.ToBase64String(ImageToByteArray(img));
If you read that string back from the file into str, you can rebuild the byte array with:
byte[] imageBytes = Convert.FromBase64String(str);
Another advantage in your particular case is that there are PHP functions for dealing with base 64 strings.

How to convert Image to base64 format without Losing of Image Quality?

I have implemented two Winform Application,One application for Image to base64 Conversion and another for base64 string to Image Conversion.First i converted image to base64 string format.
Output of above application is base64 string.
for e.g code is
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;
}
}
In my another application(base64 to Image conversion) i converted base64 string to image,the output of this application is image.
for e.g. code is
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;
}
The quality of this image is poor compare to previous image .how to resolve this?
The quality of this image is poor compare to previous image .how to resolve this?
Pass in an ImageFormat which doesn't lose quality, basically. This has nothing to do with the base64 encoding part - that's just a way of converting binary data to text and back. It's lossless.
To prove this to yourself, just save an image to a MemoryStream, rewind it and then load it from the stream - you'll see exactly the same loss of quality. Fix that, and the improvement will still be present when you use base64 to encode it as text.

C# - High Quality Byte Array Conversion of Images

I am converting images to byte array and storing in a text file using the following code. I am retrieving them successfully as well.
My concern is that the quality of the retrieved image is not up to the expectation. Is there a way to have better conversion to byte array and retrieving? I am not worried about the space conception.
Please share your thoughts.
string plaintextStoringLocation = #"D:\ImageSource\Cha5.txt";
string bmpSourceLocation = #"D:\ImageSource\Cha50.bmp";
////Read image
Image sourceImg = Image.FromFile(bmpSourceLocation);
////Convert to Byte[]
byte[] clearByteArray = ImageToByteArray(sourceImg);
////Store it for future use (in plain text form)
StoreToLocation(clearByteArray, plaintextStoringLocation);
//Read from binary
byte[] retirevedImageBytes = ReadByteArrayFromFile(plaintextStoringLocation);
//Retrieve from Byte[]
Image destinationImg = ByteArrayToImage(retirevedImageBytes);
//Display Image
pictureBox1.Image = destinationImg;
EDIT: And the solution is - use Base64
//Plain Text Storing Location
string plaintextStoringLocation = #"D:\ImageSource\GirlInflower23.txt";
string bmpSourceLocation = #"D:\ImageSource\GirlInflower1.bmp";
////Read image
Image sourceImg = Image.FromFile(bmpSourceLocation);
string base64StringOfIMage = ImageToBase64(sourceImg, ImageFormat.Bmp);
byte[] byteOfString = Convert.FromBase64String(base64StringOfIMage);
StoreToLocation(byteOfString, plaintextStoringLocation);
byte[] retrievedBytesForStrimngForImage = ReadByteArrayFromFile(plaintextStoringLocation);
MemoryStream memStream = new MemoryStream(retrievedBytesForStrimngForImage);
//memStream.Read();
Image retrievedImg = Image.FromStream(memStream);
pictureBox1.Image = retrievedImg;
Yes, it is possible to get completely lossless storage. If you just store it in its original BMP format there will be no problem. I assume you are converting it to text because you want to send it via some protocol where binary characters will be corrupted.
Instead of whatever you are doing, you could consider using Convert.ToBase64String.
I haven't had any problems with this fragment...try it...if you get good results then the problem is in your Image -> byte[] or byte[] -> Image code :)
Image srcImage;
Image destImage;
// load an image
srcImage = Image.FromFile(filename);
// save the image via stream -> byte[]
using(MemoryStream stream = new MemoryStream()){
image.Save(stream, ImageFormat.xxx);
byte[] saveArray = stream.ToArray();
/*..... strore saveArray......*/
}
// rehydrate
byte[] loadArray = /*...get byte array from storage...*/
using(MemoryStream stream = new MemeoryStream(loadArray)){
destImage = Image.FromStream(stream);
}
pictureBox.Image = dstImage;
// don't forget...dispose of any Image/Stream objects

Categories