Solve Transparent/Black background BMP image file generate via C# code

Saranya Karthik 40 Reputation points
2024-04-15T09:56:10.5566667+00:00

I create to design membership card with data(memebership ID, member photo, Firstname,lastnema, expire dateand level) via using CanvasDevice to draw all text with position declaration. after i generate to save (BMP) image to local storage, the stored image will like fully black background. I am not set any background and set transparent color but image shows like that.. i need a BMP image, sample code is below

int dpi = 96; // DPI (dots per inch)

        int cardWidth = 648;

        int cardHeight = 1016;

        CanvasDevice device = CanvasDevice.GetSharedDevice();

        CanvasRenderTarget renderTarget = new CanvasRenderTarget(device, cardWidth, cardHeight, dpi);

        // Clear the canvas with transparent color

        using (CanvasDrawingSession ds = renderTarget.CreateDrawingSession())

        {

            // Draw text

            ds.DrawText($"Test Data", 233, 200, Windows.UI.Colors.Black, new CanvasTextFormat { FontSize = 40, FontFamily = "Helvetica Neue" });

        }

        // Save the image to local storage as a BMP file

        StorageFolder printingCardsFolder = await storageFolder.CreateFolderAsync("resources", CreationCollisionOption.OpenIfExists);

        StorageFile file = await printingCardsFolder.CreateFileAsync($"Test_Card.bmp", CreationCollisionOption.ReplaceExisting);

        using (IRandomAccessStream fileStream = await file.OpenAsync(FileAccessMode.ReadWrite))

        {

            //BitmapEncoder encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.BmpEncoderId, fileStream);

            //encoder.SetPixelData(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Ignore,

            //    (uint)cardWidth, (uint)cardHeight,

            //    dpi, dpi,

            //    canvasBitmap.GetPixelBytes());

            //await encoder.FlushAsync();

            await renderTarget.SaveAsync(fileStream, CanvasBitmapFileFormat.Bmp);

        }

        // Set the saved image as the source of MembershipCardImage

        BitmapImage bitmapImage = new BitmapImage(new Uri(file.Path));

        MembershipCardImage.Source = bitmapImage;
Universal Windows Platform (UWP)
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,266 questions
0 comments No comments
{count} votes

Accepted answer
  1. Olaf Helper 40,896 Reputation points
    2024-04-15T10:02:16.5833333+00:00

    Default BMP file format don't support transparency, see https://en.wikipedia.org/wiki/BMP_file_format

    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Junjie Zhu - MSFT 15,056 Reputation points Microsoft Vendor
    2024-04-16T07:39:49.98+00:00

    Hello @Saranya Karthik ,

    Welcome to Microsoft Q&A!

    As Olaf said, BMP file cannot have transparency. It's inherent in the file format.

    If you need to modify the background color, it is recommended to use CanvasDrawingSession.FillRectangle to draw the background color.

    using (CanvasDrawingSession ds = renderTarget.CreateDrawingSession())
    {
       ds.FillRectangle(new Rect(0, 0, cardWidth, cardHeight), Colors.White);          
       // Draw text
       ds.DrawText($"Test Data", 233, 200, Windows.UI.Colors.Black, new CanvasTextFormat { FontSize = 40, FontFamily = "Helvetica Neue" });
    }
    

    Thank you.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments