将位图保存为300dpi png

时间:2014-03-14 15:48:40

标签: c# system.drawing

我正在尝试将System.Drawing.Bitmap保存为300dpi的PNG图像。似乎即使我在调用save之前使用SetResolution()来设置dpi,当我在paint中打开图像时它会报告96dpi。

MemoryStream targetStream = new MemoryStream();
using (Bitmap bitmap = rasterizer.GetCrop(
                GraphicHelpers.GetRelativeRectangle(tile, size.Width, size.Height), PixelFormat.Format32bppPArgb))
{
    using (Bitmap drawing = new Bitmap(bitmap.Width, bitmap.Height))
    {
        drawing.SetResolution(300, 300);
        using (Graphics g = Graphics.FromImage(drawing))
        {
            g.DrawImage(drawing, new Rectangle(0, 0, bitmap.Width, bitmap.Height), new Rectangle(0, 0, bitmap.Width, bitmap.Height), GraphicsUnit.Pixel);

            drawing.Save(targetStream, ImageFormat.Png);
        }
    }
}

我尝试使用SetResolution()然后只调用save指定ImageFormat.Png,但这似乎也没有。

使用GDI创建300dpi png需要什么?

1 个答案:

答案 0 :(得分:0)

试试这种方式

Single rapportRes = 300 / 96; //where 96 is standard screen resolution
MemoryStream targetStream = new MemoryStream();
using (Bitmap bitmap = rasterizer.GetCrop(GraphicHelpers.GetRelativeRectangle(tile, int(size.Width*rapportRes), int(size.Height*rapportRes)), PixelFormat.Format32bppPArgb))
    {
        using (Bitmap drawing = new Bitmap(int(bitmap.Width*rapportRes), int(bitmap.Height*rapportoRes)))
        {
            drawing.SetResolution(300, 300);
            using (Graphics g = Graphics.FromImage(drawing))
            {
                g.DrawImage(drawing, new Rectangle(0, 0, int(bitmap.Width*rapportRes), int(bitmap.Height*rapportoRes)), new Rectangle(0, 0, int(bitmap.Width*rapportRes), int(bitmap.Height*rapportoRes)), GraphicsUnit.Pixel);

                drawing.Save(targetStream, ImageFormat.Png);
            }
        }
    }