在CentOS上的Mono中,位图无法正确显示

时间:2018-06-01 15:33:08

标签: c# asp.net mono

在CentOS上安装Mono,启动xsp4,并显示以下aspx 虽然显示图像,但Windows和CentOS的结果不同,但我不知道原因。

Windows Mono显示的图像是正确的 每次访问时,CentOS图像的显示都不同。

有人知道吗?

<%@ Page Language="C#" %>

<%@ Import Namespace="System.Drawing" %>
<%@ Import Namespace="System.Runtime.InteropServices" %>

<script runat="server">
    Bitmap bmp = null;

    protected void Page_Load(Object source, EventArgs e) {
        System.Drawing.Image img = System.Drawing.Image.FromFile(@"./test.png");
        bmp = new Bitmap(img);

        bmp = Bin(bmp);

        Response.ContentType = "image/png";
        Response.Flush();
        bmp.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Png);
        Response.End();
    }

    public static Bitmap Bin(Bitmap src)
    {
        Bitmap dest =
            new Bitmap(
                src.Width, src.Height, System.Drawing.Imaging.PixelFormat.Format1bppIndexed);

        System.Drawing.Imaging.BitmapData srcBitmapData =
            src.LockBits(
                new Rectangle(0, 0, src.Width, src.Height),
                System.Drawing.Imaging.ImageLockMode.WriteOnly, src.PixelFormat);

        System.Drawing.Imaging.BitmapData destBitmapData =
            dest.LockBits(
                new Rectangle(0, 0, dest.Width, dest.Height),
                System.Drawing.Imaging.ImageLockMode.WriteOnly, dest.PixelFormat);

        byte[] srcPixels = new byte[srcBitmapData.Stride * src.Height];
        Marshal.Copy(srcBitmapData.Scan0, srcPixels, 0, srcPixels.Length);

        byte[] destPixels = new byte[destBitmapData.Stride * destBitmapData.Height];
        for (int y = 0; y < destBitmapData.Height; y++)
        {
            for (int x = 0; x < destBitmapData.Width; x++)
            {
                if (128 <= ConvertToGrayscale(srcPixels, x, y, srcBitmapData.Stride))
                {
                    int pos = (x >> 3) + destBitmapData.Stride * y;
                    destPixels[pos] |= (byte)(0x80 >> (x & 0x7));
                }
            }
        }
        Marshal.Copy(destPixels, 0, destBitmapData.Scan0, destPixels.Length);

        src.UnlockBits(srcBitmapData);
        dest.UnlockBits(destBitmapData);

        return dest;
    }

    const int RedFactor = (int)(0.298912 * 1024);
    const int GreenFactor = (int)(0.586611 * 1024);
    const int BlueFactor = (int)(0.114478 * 1024);

    private static float ConvertToGrayscale(byte[] srcPixels, int x, int y, int stride)
    {
        int position = x * 3 + stride * y;
        byte b = srcPixels[position + 0];
        byte g = srcPixels[position + 1];
        byte r = srcPixels[position + 2];

        return (r * RedFactor + g * GreenFactor + b * BlueFactor) >> 10;
    }

</script>

CentOS7
enter image description here

Windows10
enter image description here

1 个答案:

答案 0 :(得分:0)

如果将ImageLockModeWriteOnly更改为ReadWrite,我将得到想要的东西。