首页 文章

C#:尝试向图像添加缩略图符号时,结果会有所不同

提问于
浏览
0

我正在自动调整ASP.NET应用程序中的图像大小,以便创建该图像的低分辨率缩略图 . 这段代码工作正常 . 调整大小后,我尝试向该图像添加“缩略图符号”,例如小放大镜或加号,但结果因图像大小而异 .

请注意:原始图像仅调整为一定宽度,因此图像的高度不同 .

我的代码如下所示:

private static byte[] InsertThumbnailSign(byte[] imageBuffer, string signPath)
{
    byte[] output = null;
    MemoryStream stream = new MemoryStream(imageBuffer);
    Image image = Image.FromStream(stream);

    // Add the thumbnail-sign
    Image thumbNailSign = Image.FromFile(signPath);
    Graphics graphic = Graphics.FromImage(image);
    graphic.DrawImageUnscaled(thumbNailSign, image.Width - thumbNailSign.Width - 4, image.Height - thumbNailSign.Height - 4);
    graphic.Flush();

    MemoryStream memoryStream = new MemoryStream();
    image.Save(memoryStream, ImageFormat.Jpeg);
    output = new byte[memoryStream.Length];
    memoryStream.Position = 0;
    memoryStream.Read(output, 0, (int)memoryStream.Length);
    memoryStream.Close();

    stream.Dispose();
    graphic.Dispose();
    memoryStream.Dispose();

    return output;
}

在我看来,缩略图标志应该有一个恒定的大小,但事实并非如此 . 你有任何想法如何实现这一目标吗?

编辑:刚编辑代码以了解不同的分辨率 . 但它仍然不起作用:

private static byte[] InsertThumbnailSign(byte[] imageBuffer, string signPath)
{
    byte[] output = null;
    MemoryStream stream = new MemoryStream(imageBuffer);
    Image image = Image.FromStream(stream);

    // Add the thumbnail sign with resolution of the containing image
    Bitmap t = (Bitmap)Bitmap.FromFile(signPath);
    t.SetResolution(image.HorizontalResolution, image.VerticalResolution);
    Image thumbNailSign = t;

    Graphics graphic = Graphics.FromImage(image);
    graphic.DrawImageUnscaled(thumbNailSign, image.Width - thumbNailSign.Width - 4, image.Height - thumbNailSign.Height - 4);
    graphic.Flush();

    MemoryStream memoryStream = new MemoryStream();
    image.Save(memoryStream, ImageFormat.Png);
    output = new byte[memoryStream.Length];
    memoryStream.Position = 0;
    memoryStream.Read(output, 0, (int)memoryStream.Length);
    memoryStream.Close();

    stream.Dispose();
    graphic.Dispose();
    memoryStream.Dispose();

    return output;
}

2 回答

  • 0

    确保在调整大小之前将所有图像标准化为一致的DPI / PPI和/或在创建要覆盖的“符号”图像时使用该DPI .

    300 DPI 2“宽图像宽度为600像素,如果DPI设置为72 DPI,您还可以在600像素宽的图像中使用宽度超过8英寸的图像 .

    请参阅this question,其中包含带有示例代码的link,以获取有关图形对象的更多详细信息 .

  • 0

    TombMedia是对的 . 决议一直是重点 .

    我在Resizing-Algorithm中发现了一个错误,导致firefox调整图像大小并导致插入图像的大小不同 .

    修复该错误并调整分辨率后,它可以正常工作 . 谢谢你的提示!

相关问题