无法将.bmp图像从ASP.NET保存到服务器

时间:2013-01-18 23:10:40

标签: c# asp.net iis-7 windows-server-2008 gdi

在ASP.net(C#)Web应用程序中,我使用HTML 5 canvas和java脚本创建了一个签名板。我们正在使用触摸设备(Android和iOS)捕获签名。

签名完成后,它会将数据从画布发送到服务器,然后将其转换为.bmp图像,并使用Bitmap.Save将其保存到服务器上根程序目录中名为签名文件的目录中。 ,格式)。到目前为止,这对几个客户来说已经好几个月了。

不幸的是,我目前正在捕捉异常,但没有将它们发送到任何地方(我目前正在纠正的巨大错误)。如果必须的话,我将使用一些异常输出重新编译代码,以便我可以获得更多信息,但由于它是一个新的客户端,我希望能够尽快解决这个问题,因为代码已经工作了很长时间没有问题,我很乐观它的服务器设置。

IIS7或Windows Server 2008中是否存在可能阻止应用程序将文件保存到服务器的安全设置。如果没有,可能需要在服务器上安装一些东西以允许以下内容:

byte[] imageBytes = Convert.FromBase64String(suffix);

MemoryStream ms = new MemoryStream(imageBytes, 0, imageBytes.Length);

ms.Write(imageBytes, 0, imageBytes.Length);

string thePath = Path.GetDirectoryName(saveLocation);

FileStream fs = new FileStream(thePath + @"\image.png", FileMode.Create);

BinaryWriter bw = new BinaryWriter(fs);

bw.Write(imageBytes);

bw.Close();

    if (string.IsNullOrEmpty(Path.GetFileName(saveLocation)))
        {

            string filename = Path.GetRandomFileName();
            filename = filename.Substring(0, filename.IndexOf('.'));
            filename = filename + Path.GetRandomFileName();
            filename = filename.Substring(0, filename.IndexOf('.'));

            theSaveLocation = saveLocation + @"\" + filename + "." + format;
        }


        ms = new MemoryStream();

        Bitmap input = (Bitmap)Bitmap.FromFile(thePath + @"\image.png");

        Bitmap result = ProcessBitmap(input, Color.White);

    result = (Bitmap)resizeImage((System.Drawing.Image)result, size);

    result.Save(theSaveLocation, System.Drawing.Imaging.ImageFormat.Bmp);

    input.Dispose();
        result.Dispose();
        ms.Close();

这是我上面使用的过程Bitmap函数

private Bitmap ProcessBitmap(Bitmap bitmap, Color color)
{
    Bitmap temp = new Bitmap(bitmap.Width, bitmap.Height, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
    Graphics g = Graphics.FromImage(temp);
    g.Clear(color);
    g.DrawImage(bitmap, Point.Empty);
    return temp;
}

这是我上面使用的resizeImage函数:

private static System.Drawing.Image resizeImage(System.Drawing.Image imgToResize, Size size)
{

    int sourceWidth = imgToResize.Width;
    int sourceHeight = imgToResize.Height;

    float nPercent = 0;
    float nPercentW = 0;
    float nPercentH = 0;

    nPercentW = ((float)size.Width / (float)sourceWidth);
    nPercentH = ((float)size.Height / (float)sourceHeight);

    if (nPercentH < nPercentW)
        nPercent = nPercentH;
    else
        nPercent = nPercentW;

    int destWidth = (int)(sourceWidth * nPercent);
    int destHeight = (int)(sourceHeight * nPercent);


    Bitmap b = new Bitmap(destWidth, destHeight);
    Graphics g = Graphics.FromImage((System.Drawing.Image)b);


    g.DrawImage(imgToResize, 0, 0, destWidth, destHeight);
    g.Dispose();

    return (System.Drawing.Image)b;
}

0 个答案:

没有答案
相关问题