I need BMP image with transparent backgorund to pass the image to Evolis card printer, but BMP cannot accept transparent background, so i found using PNG with transparent with (300 dpi and 24 bpp) convert to BMP via UWP APP
Loading
I need BMP image with transparent backgorund to pass the image to Evolis card printer, but BMP cannot accept transparent background, so i found using PNG with transparent with (300 dpi and 24 bpp) convert to BMP via UWP APP
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Vijay Pratap SinghRank #381 · 6.6K reputationPosted Apr 17, 2024, 12:26 AM EDT
Here's a step-by-step guide to achieving this:
System.Drawing.using System.Drawing; using System.Drawing.Imaging; class Program { static void Main(string[] args) { // Create a PNG image with transparent background Bitmap pngImage = new Bitmap(200, 200, PixelFormat.Format32bppArgb); using (Graphics graphics = Graphics.FromImage(pngImage)) { graphics.Clear(Color.Transparent); // Draw something on the image if needed graphics.DrawString("Transparent PNG", new Font("Arial", 12), Brushes.Black, new PointF(10, 10)); } // Save the PNG image pngImage.Save("transparent_image.png", ImageFormat.Png); // Load the PNG image using (Bitmap pngImageLoaded = new Bitmap("transparent_image.png")) { // Create a new BMP image with the desired properties Bitmap bmpImage = new Bitmap(pngImageLoaded.Width, pngImageLoaded.Height, PixelFormat.Format24bppRgb); bmpImage.SetResolution(300, 300); // Set DPI to 300 // Draw the PNG image onto the BMP image to remove transparency using (Graphics graphics = Graphics.FromImage(bmpImage)) { graphics.Clear(Color.White); // Set background to white graphics.DrawImage(pngImageLoaded, 0, 0); } // Save the BMP image bmpImage.Save("converted_image.bmp", ImageFormat.Bmp); } } }Hope this will help
Naimish MakwanaRank #324 · 7.7K reputationPosted Apr 17, 2024, 12:22 AM EDT
You want to create a BMP image from a PNG image with a transparent background, using a UWP app. Here is a sample code snippet in C# that uses the
Windows.Graphics.Imagingnamespace to convert a PNG image to a BMP image:using System; using System.IO; using System.Threading.Tasks; using Windows.Graphics.Imaging; using Windows.Storage; using Windows.Storage.Streams; public async Task ConvertPngToBmpAsync(string inputFilePath, string outputFilePath) { // Get the input file StorageFile inputFile = await StorageFile.GetFileFromPathAsync(inputFilePath); // Get a stream from the input file using (IRandomAccessStream inputFileStream = await inputFile.OpenAsync(FileAccessMode.Read)) { // Decode the input file as a bitmap BitmapDecoder decoder = await BitmapDecoder.CreateAsync(inputFileStream); // Get the first frame of the bitmap BitmapFrame bitmapFrame = await decoder.GetFrameAsync(0); // Convert the bitmap to a SoftwareBitmap SoftwareBitmap softwareBitmap = await bitmapFrame.GetSoftwareBitmapAsync(); // Get the output file StorageFile outputFile = await StorageFile.GetFileFromPathAsync(outputFilePath); // Get a stream from the output file using (IRandomAccessStream outputFileStream = await outputFile.OpenAsync(FileAccessMode.ReadWrite)) { // Create a bitmap encoder BitmapEncoder encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.BmpEncoderId, outputFileStream); // Set the software bitmap encoder.SetSoftwareBitmap(softwareBitmap); // Set the DPI encoder.BitmapTransform.InterpolationMode = BitmapInterpolationMode.Linear; encoder.BitmapTransform.ScaledWidth = (uint)(softwareBitmap.PixelWidth * 300 / decoder.DpiX); encoder.BitmapTransform.ScaledHeight = (uint)(softwareBitmap.PixelHeight * 300 / decoder.DpiY); // Flush the data to the output file await encoder.FlushAsync(); } } }This code reads a PNG image, decodes it into a
SoftwareBitmap, and then encodes thatSoftwareBitmapinto a BMP image with a DPI of 300. The output BMP image is saved to the specified output file path.Please note that BMP does not support transparency. If the input PNG image has transparent areas, they will be filled with black in the output BMP image. Also, please ensure that you have the necessary permissions to read and write files in the specified paths.
This code is for a UWP app, so it uses the
Windows.StorageandWindows.Graphics.Imagingnamespaces, which might not be available in other types of C# applications. If you’re not developing a UWP app, you might need to use a different approach.Remember to call this method from an async method, or use
Task.Runto run it from a synchronous method. For example:Please replace
inputFilePathandoutputFilePathwith your actual file paths.Thanks