SKBitmap and DPI

Hi guys, I'm just starting with SkiaSharp in a .NET Core 2.0 project. I need to create a bitmap and save it with a specified dpi. I've played with saving a SKBitmap in png format and works just fine but I cannot see how to specify the DPI/resolution. If I open the png I see it was saved with 96dpi setting.... does Skia support setting any output dpi other than that?

Best Answer


  • mattleibowmattleibow ZAXamarin Team Xamurai

    To be honest, I am not sure and I don't think I have noticed any specific feature.
    Does the PNG codec support DPI, or is it just via increased resolution?

    You can ask on the official skia forums:!forum/skia-discuss Google should know the answer.

    If this is just a matter of scale, you can always create a larger image, and then apply a scale before drawing anything. This will result in a larger image, and all the draw operations can stay the same:


    var bmp = new SKBitmap(100, 100);
    var canvas = new SKCanvas(bmp);
    // draw
    canvas.DrawRect(0, 0, 100, 100);


    var bmp = new SKBitmap(300, 300);
    var canvas = new SKCanvas(bmp);
    // scale to compensate for the larger bitmap
    // draw
    canvas.DrawRect(0, 0, 100, 100);

    You can use the same draw operations, but this result is larger.

  • GMGMGMGM ARMember ✭✭
    Accepted Answer

    Thanks for your reply @mattleibow
    Well, I'm already scaling the bitmap to simulate the dpi resolution but the problem here is that setting should be stored in the output png image file itself which is useful for printing purposes. Looking at the web, it seems that PNG spec supports it
    I'll try change those bytes through code after getting the byte array from Skia codec. By the way, it seems that Skia does not support it!searchin/skia-discuss/dpi|sort:relevance/skia-discuss/nY4tLHvjkVs/8vcioy_e8z4J

  • mattleibowmattleibow ZAXamarin Team Xamurai
    edited September 2017

    I suspected as much.

    But, your plan of modifying the data is a good one. SkiaSharp is really a image drawing library (create an image in memory from draw commands). You can (and probably should) use another library for advanced encoding - SkiaSharp really just supports basic png and jpeg encoding.

    I am not sure of libraries and such, but you can just take the actual memory of the bitmap, and pass that to whatever encoder you want:

    SKBitmap bmp = ...;
    // get the pointer to the pixels
    IntPtr pixels = bmp.GetPixels();

    This way, you can make use of other libraries to take the pixel data and convert it to bmp, gif, and other comprehensive codecs or configurations.

  • ThomasFlemmingThomasFlemming DEMember ✭✭

    @GMGM said:
    I'll try change those bytes through code after getting the byte array from Skia codec.

    Did you manage changing those bytes?
    Would you mind share this piece of code?

    Thanks, Tom

Sign In or Register to comment.