GDI +中发生一般错误. C#电子邮件服务 [英] A generic error occurred in GDI+. C# Email Service

查看:64
本文介绍了GDI +中发生一般错误. C#电子邮件服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的所有人,

我正在使用下面的代码来获取字节流并保存图像.

但现在我面临一个问题.它引发了异常. "GDI +中发生一般错误"

当我构建它时,它不会给出任何错误.如果我手动运行exe也很好,但是当我在任务计划程序中计划它(我的服务器是Windows 2008)时,它给了我这个例外.

请找到我下面的代码.

Dear All,

I am using the below code to get a byte stream and save an image.

but now I am facing a problem. it is throwing an exception. "A generic error occurred in GDI+"

It doesn''t give any errors when I build it. if I run the exe manually also it is fine, but when I schedule it in task scheduler (my server is Windows 2008) it gives me this exception.

please find my below code.

public string byteArrayToImage(byte[] byteArrayIn)
        {
            try
            {

                if (byteArrayIn != null)
                {
                    MemoryStream ms = new MemoryStream(byteArrayIn);
                    Image returnImage = Image.FromStream(ms);
                    guid = Guid.NewGuid();
                    _imageName = guid.ToString();
                    _fullImagePath = "images/" + _imageName + ".jpg";
                    returnImage.Save(_fullImagePath, System.Drawing.Imaging.ImageFormat.Jpeg);
                    returnImage.Dispose();

                }
            }
            catch (Exception ex)
            {
                UpdateEmailStatus("Sending Failed-byteArrayToImage3" + " " + ex.Message);
            }
            return _fullImagePath;

        }


请帮助我.
我明天有一个生产版本.


please help me on this.
I have a production release tomorrow.

thanks.

推荐答案

"GDI +中发生一般错误"消息基本上意味着,无论字节数组包含什么,都无法将其识别为图像:
The "A generic error occurred in GDI+" message basically means that whatever your array of bytes contains, it is not recognisable as an image:
Image returnImage = Image.FromStream(ms);

失败.

检查数据-您是否传递了图像文件的名称而不是其内容?

[edit]
我必须仔细阅读问题!

机会是,如果它可以正常工作,则无法按计划执行,这不是问题所在,而是映像保存-就是保存.

计划任务不能与普通"应用程序以同一用户运行,并且极有可能您要保存文件的文件夹没有执行计划任务的用户的写许可权.

将文件夹权限更改为任何用户的完全访问权限",就可以了.
[/edit]

Is failing.

Check your data - have you passed the name of a image file instead of it''s content?

[edit]
I must read questions a bit more carefully!

The chances are that if it works normally, then fails on scheduled execution it isn''t the image load that is the problem - it''s the save.

Scheduled tasks do not run with the same user as "normal" applications, and there is a very good chance that the folder you are trying to save the file to does not have write permissions for the user that is executing the scheduled task.

Change the folder permissions to "any user full access" and you should be fine.
[/edit]


我将这段代码用于图像保存,也许会对您有所帮助!

I used this code for image saving i might help you maybe!

using System.Drawing.Imaging;
using System.Drawing;
using System.IO;

public static void SaveJpeg
(string path, Image img, int quality)
{
EncoderParameter qualityParam
= new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, quality);

ImageCodecInfo jpegCodec
= GetEncoderInfo(@"image/jpeg");

EncoderParameters encoderParams
= new EncoderParameters(1);

encoderParams.Param[0] = qualityParam;

System.IO.MemoryStream mss = new System.IO.MemoryStream();

System.IO.FileStream fs
= new System.IO.FileStream(path, System.IO.FileMode.Create
, System.IO.FileAccess.ReadWrite);

img.Save(mss, jpegCodec, encoderParams);
byte[] matriz = mss.ToArray();
fs.Write(matriz, 0, matriz.Length);

mss.Close();
fs.Close();
}


这篇关于GDI +中发生一般错误. C#电子邮件服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆