控制台应用程序中的GDI +发生了一般错误

时间:2014-06-21 11:33:31

标签: c# asp.net-mvc

我制作了一个控制台应用程序,其中我预先生成了所有图像,就像nopCommerce生成图像一样。

这是我的代码:

  thumbFileName = !String.IsNullOrEmpty(seoFileName) ?
                       string.Format("{0}_{1}_{2}.{3}", pictureId.ToString("0000000"), seoFileName, _productThumbPictureSize, lastPart) :
                       string.Format("{0}_{1}.{2}", pictureId.ToString("0000000"), _productThumbPictureSize, lastPart);

            if (_generatePictures)
            {
                if (storeInDb)
                {
                    storeInDb = GetSettingByKey<bool>("media.images.storeindb");
                }

                // byte defaultImageQuality = GetSettingByKey<byte>("mediasettings.defaultimagequality");

                if (storeInDb)
                {
                    pictureBinary = GetPictureByProductId(Id);
                }

                else
                {
                    pictureBinary = LoadPictureFromFile(pictureId, mimeType);
                }

                if (pictureBinary == null || pictureBinary.Length == 0)
                {
                    url = GetDefaultPictureUrl(_productThumbPictureSize);
                    return url;
                }

                if (isNew)
                {
                    DeletePictureThumbs(pictureId);

                    //we do not validate picture binary here to ensure that no exception ("Parameter is not valid") will be thrown
                    var picture = UpdatePicture(pictureId,
                          pictureBinary,
                          mimeType,
                         seoFileName,
                          false);
                }

                if (pictureBinary.Length != 0)
                {
                    //Generating Images
                    string newpath = "Thumbs";
                    var _path = Path.Combine(_imagesPath, newpath);
                    var thumbFilePath = GetPictureLocalPath(thumbFileName, _path);//"C:\\Users\\Developer\\Documents\\Server2\\Solr Plugin Branch Nop-310\\Presentation\\Nop.Web\\Content\\Images\\Thumbs\\"+thumbFileName;
                    if (!File.Exists(thumbFilePath))
                    {
                        using (var stream = new MemoryStream(pictureBinary))
                        {
                            Bitmap b = null;

                            try
                            {
                                //try-catch to ensure that picture binary is really OK. Otherwise, we can get "Parameter is not valid" exception if binary is corrupted for some reasons
                                b = new Bitmap(stream );

                            }
                            catch (ArgumentException exc)
                            {
                                string msg = exc.ToString();
                                string fullmsg = string.Format("Error generating picture thumb. ID={0}", pictureId);
                                InsertSystemLog(msg, fullmsg);
                            }


                            if (b == null)
                            {
                                //bitmap could not be loaded for some reasons
                                return url;
                            }
                            var newSize = CalculateDimensions(b.Size, _productThumbPictureSize);

                            if (newSize.Width < 1)
                                newSize.Width = 1;
                            if (newSize.Height < 1)
                                newSize.Height = 1;

                            using (var newBitMap = new Bitmap(newSize.Width, newSize.Height))
                            {
                                using (var g = Graphics.FromImage(newBitMap))
                                {
                                    g.SmoothingMode = SmoothingMode.HighQuality;
                                    g.InterpolationMode = InterpolationMode.HighQualityBicubic;
                                    g.CompositingQuality = CompositingQuality.HighQuality;
                                    g.PixelOffsetMode = PixelOffsetMode.HighQuality;
                                    g.DrawImage(b, 0, 0, newSize.Width, newSize.Height);
                                    var ep = new EncoderParameters();
                                    ep.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 80L);
                                    ImageCodecInfo ici = GetImageCodecInfoFromExtension(lastPart);
                                    if (ici == null)
                                        ici = GetImageCodecInfoFromMimeType("image/jpeg");
                                    try
                                    {

                                             newBitMap.Save(thumbFilePath, ici, ep);


                                    }
                                    catch (ArgumentException exc)
                                    {
                                        string msg = exc.ToString();
                                        string fullmsg = string.Format("Unable to save Picture ID={0}", pictureId);
                                        InsertSystemLog(msg, fullmsg);
                                    }
                                }
                            }
                            b.Dispose();
                        }
                    }
                }

我在下一行收到了GDI +错误:

 b = new Bitmap(stream);

错误:

The log entry message.Short message: A generic error occurred in GDI+. 
The details for the log entry.Full message: at System.Drawing.Image.FromStream(Stream stream)

请注意,所有图像都保存在文件系统中。并且文件夹具有写入权限。并且此错误仅在实时站点上发生,而不是在localhost。

0 个答案:

没有答案