How I get 300 DPI with 648 X 1016 Using SKBitmap

Saranya Karthik 40 Reputation points
2024-04-16T07:17:48.06+00:00

I am trying to PNG image to BMP image via using SKBitmap, the BMP image not set in 300 DPI why? below i mentioned my code, if there is any mistakes please correct my code

using (SKBitmap pngBitmap = SKBitmap.Decode(inputFilePath))

            {

                // Calculate the scaling factor for 300 DPI


                

                // Calculate the scaled dimensions for the BMP bitmap

                int bmpWidth = pngBitmap.Width;

                int bmpHeight = pngBitmap.Height;

                // Create a new SKBitmap with the scaled dimensions

                using (SKBitmap bmp = new SKBitmap(bmpWidth, bmpHeight))

                {

                    // Copy the PNG bitmap to the BMP bitmap

                    pngBitmap.CopyTo(bmp);

                    // Set the DPI for the BMP bitmap

                   float dpiScale = 300f / 96f;

                    // Save the BMP bitmap to a file

                    using (SKImage img = SKImage.FromBitmap(bmp))

                    using (SKData data = img.Encode())

                    using (var stream = System.IO.File.OpenWrite(outputFilePath))

                    {

                        data.SaveTo(stream);

                    }

                }
Developer technologies | Universal Windows Platform (UWP)
Developer technologies | C#
{count} votes

2 answers

Sort by: Most helpful
  1. Anonymous
    2024-04-16T08:54:50.9533333+00:00

    Hi @Saranya Karthik , Welcome to Microsoft Q&A,

    You may consider using the System.Drawing namespace in .NET Framework or .NET Core.

    System.Drawing provides a set of classes and methods for processing images that can be used to achieve your target effects.

    Here is sample code implemented using System.Drawing to convert a PNG image to a BMP image and set the DPI:

    using System.Drawing;
    using System.Drawing.Imaging;
    
    string inputFilePath = "input.png";
    string outputFilePath = "output.bmp";
    
    using (Bitmap pngBitmap = new Bitmap(inputFilePath))
    {
        // Create a new Bitmap with the same dimensions as the original PNG bitmap
        Bitmap bmpBitmap = new Bitmap(pngBitmap.Width, pngBitmap.Height);
    
        // Copy the PNG bitmap to the BMP bitmap
        using (Graphics graphics = Graphics.FromImage(bmpBitmap))
        {
            graphics.DrawImage(pngBitmap, 0, 0);
        }
    
        // Set the DPI for the BMP bitmap
        bmpBitmap.SetResolution(300, 300);
    
        // Save the BMP bitmap to a file
        bmpBitmap.Save(outputFilePath, ImageFormat.Bmp);
    }
    
    

    Best Regards,

    Jiale


    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

  2. Junjie Zhu - MSFT 21,646 Reputation points
    2024-04-17T10:01:57.9766667+00:00

    Hi @Saranya Karthik ,

    System.Drawing.Common is not supported on UWP.

    It is recommended that you use WriteableBitmap to read png files and save them as new bmp files.

    private async void Button_Click_1(object sender, RoutedEventArgs e)
    {
        FileOpenPicker fileOpenPicker = new FileOpenPicker();
        fileOpenPicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
        fileOpenPicker.FileTypeFilter.Add(".png");
        fileOpenPicker.ViewMode = PickerViewMode.Thumbnail;
        var inputFile = await fileOpenPicker.PickSingleFileAsync();
        if (inputFile == null)
        {
            // The user cancelled the picking operation
            return;
        }
        using (IRandomAccessStream stream = await inputFile.OpenAsync(FileAccessMode.Read))
        {
            // Create the decoder from the stream
            BitmapDecoder decoder = await BitmapDecoder.CreateAsync(stream);
            // Get the SoftwareBitmap representation of the file
            WriteableBitmap writeableBitmap = new WriteableBitmap((int)decoder.PixelWidth, (int)decoder.PixelHeight);
            await writeableBitmap.SetSourceAsync(stream);
            Windows.Storage.StorageFolder storageFolder = Windows.Storage.ApplicationData.Current.LocalFolder;
            Windows.Storage.StorageFile bmpFile = await storageFolder.CreateFileAsync("bitmap.bmp", Windows.Storage.CreationCollisionOption.ReplaceExisting);
            SaveSoftwareBitmapToFile(writeableBitmap, bmpFile);
        }
    }
    private async void SaveSoftwareBitmapToFile(WriteableBitmap softwareBitmap, StorageFile outputFile)
    {
        using (IRandomAccessStream stream = await outputFile.OpenAsync(FileAccessMode.ReadWrite))
        {
            // Create an encoder with the desired format
            BitmapEncoder encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.BmpEncoderId, stream);
                    
            var pixelStream = softwareBitmap.PixelBuffer.AsStream();
            byte[] pixels = new byte[softwareBitmap.PixelBuffer.Length];
            await pixelStream.ReadAsync(pixels, 0, pixels.Length);
            encoder.SetPixelData(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Ignore, (uint)softwareBitmap.PixelWidth, (uint)softwareBitmap.PixelHeight, 300, 300, pixels);           
                    
            await encoder.FlushAsync();
                             
        }
    }
    

    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.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.