0

我的 asp.net-Mysql 应用程序托管在 windowsserver 上,当我尝试从 admin.aspx 页面将图像插入 mysql 表时,它显示错误

“你调用的对象是空的。”

但是从我的 VS 中,我可以将图像插入到本地 mysql 服务器而不会出现任何错误。

当我从 webadmin 插入图像时,它已成功插入并在网站上显示图像。我可以通过我的 admin.aspx 更新内容,但是当我尝试更新或插入图像时,出现错误。

[NullReferenceException: Object reference not set to an instance of an object.]
   Lavande.DBconnect.addimagehome(Byte[] image, String Content, String arabiccontent) in C:\Users\user\Desktop\Lavande_Asp\Lavande\Lavande\DBconnect.cs:33
   Lavande.abklavande900.button_savehome_Click(Object sender, EventArgs e) in C:\Users\user\Desktop\Lavande_Asp\Lavande\Lavande\abklavande900.aspx.cs:84
   System.Web.UI.WebControls.Button.OnClick(EventArgs e) +118
   System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +112
   System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10
   System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
   System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +36
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +5563

以下是读取上传文件的代码:

 string path = System.IO.Path.GetFullPath(FileUpload_home.PostedFile.FileName);
Byte[] image = WM.ImageSetting("watermarkname", Server.MapPath("upload/") + "logo1.png", path);
DB.addimagehome(image, contentsend, arabiccontent);

水印类。

public class watermark
{
    public Byte[] ImageSetting(string wmText, string wmImage, string mainImage)
    {
        byte[] imageBytes = null;
        if (File.Exists(mainImage))
        {

            System.Drawing.Image image = System.Drawing.Image.FromFile(mainImage);

            Graphics graphic;
            if (image.PixelFormat != PixelFormat.Indexed && image.PixelFormat != PixelFormat.Format8bppIndexed && image.PixelFormat != PixelFormat.Format4bppIndexed && image.PixelFormat != PixelFormat.Format1bppIndexed)
            {
                // Graphic is not a Indexed (GIF) image
                graphic = Graphics.FromImage(image);
            }
            else
            {
                /* Cannot create a graphics object from an indexed (GIF) image.
                 * So we're going to copy the image into a new bitmap so
                 * we can work with it. */
                Bitmap indexedImage = new Bitmap(image);
                graphic = Graphics.FromImage(indexedImage);

                // Draw the contents of the original bitmap onto the new bitmap.
                graphic.DrawImage(image, 0, 0, image.Width, image.Height);
                image = indexedImage;
            }
            graphic.SmoothingMode = SmoothingMode.AntiAlias & SmoothingMode.HighQuality;

            //Text Watermark properties
            Font myFont = new Font("segoe script", 17, FontStyle.Bold);
            SolidBrush brush = new SolidBrush(Color.FromArgb(40, Color.White));
            SizeF textSize = new SizeF();
            if (wmText != "")
                textSize = graphic.MeasureString(wmText, myFont);

            //Image Watermark
            System.Drawing.Image ig = null;
            if (wmImage != "")
                ig = System.Drawing.Image.FromFile(wmImage);

            // Write the text watermark and image watermark across the main image.
            for (int y = 0; y < image.Height; y++)
            {
                for (int x = 0; x < image.Width; x++)
                {
                    PointF pointF = new PointF(x, y);
                    PointF pointFm = new PointF(x, y+150);
                    if (wmText != "")
                    {
                        graphic.DrawString(wmText, myFont, brush, pointFm);
                        x += Convert.ToInt32(textSize.Width);
                    }
                    if (wmImage != "")
                    {
                        graphic.DrawImage(ig, pointF);
                        x += Convert.ToInt32(ig.Width);
                    }
                }
                if (wmText != "")
                    y += Convert.ToInt32(textSize.Height);
                if (wmImage != "")
                    y += Convert.ToInt32(ig.Height);

            }

            using (MemoryStream memoryStream = new MemoryStream())
            {
                // save image in memoryStream with it format which get it from GetImageFormat function
                image.Save(memoryStream, GetImageFormat(mainImage));
                imageBytes = memoryStream.ToArray();
            }
            graphic.Dispose();
        }


        return imageBytes;


    }

    //function to return Image Format
    ImageFormat GetImageFormat(String path)
    {
        switch (Path.GetExtension(path).ToLower())
        {
            case ".bmp": return ImageFormat.Bmp;
            case ".gif": return ImageFormat.Gif;
            case ".jpg": return ImageFormat.Jpeg;
            case ".png": return ImageFormat.Png;
            default: return null;
        }
    }

}

}

4

2 回答 2

0

根据您的堆栈跟踪及其指向的代码,该image变量必须为空。当您尝试查看该Length属性时会引发异常...没有可用于检查该属性的对象。

它为空的原因是您的ImageSetting()函数可以总结如下:

Byte[] ImageSetting(...)
{
    Byte[] imageBytes = null;
    if (File.Exists(...) )
    {
        //...
    }
    return imageBytes;
}

显然, File.Exists() 调用失败,因此您不返回任何图像。您可能应该有一个可以加载和使用的占位符图像。此外,您不应该首先使用 File.Exists(),因为文件系统是易失的。相反,将您的编码工作投入到异常处理程序中。

于 2012-12-01T15:52:31.160 回答
0

就像我在评论中所写:无法从服务器检索您的图像,因为您的池身份无权访问该目录。

将包含图像的目录的访问权限设置为所有人,然后它可能会起作用(仅用于测试目的)。

于 2012-12-01T15:53:39.087 回答