0
votes

i was trying to save a bitmap as jpg but i could not manage. it throws the excpetion as below

: System.Runtime.InteropServices.ExternalException: [ExternalException (0x80004005): GDI+ içinde genel bir hata oluştu.]
System.Drawing.Image.Save(String filename, ImageCodecInfo encoder, EncoderParameters encoderParams) +477588
httpdocs_Deneme.Page_Load(Object sender, EventArgs e) in c:\inetpub\vhosts\tufanyumlu.com\httpdocs\Deneme.aspx.cs:47
System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +51 System.Web.UI.Control.OnLoad(EventArgs e) +95
System.Web.UI.Control.LoadRecursive() +59
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +678

my code block is below

protected void Page_Load(object sender, EventArgs e)
{
    WebClient wc = new WebClient();
    byte[] originalData = wc.DownloadData("http://tufanyumlu.com/images/00050-900x596.jpg");
    MemoryStream stream = new MemoryStream(originalData);
    Bitmap bitMapImage = new Bitmap(stream);
    Graphics graphicImage = Graphics.FromImage(bitMapImage);
    graphicImage.SmoothingMode = SmoothingMode.AntiAlias;
    graphicImage.DrawLine(new Pen(Color.White, 65), 650, 560, 900, 560);
    graphicImage.DrawString("SgSHoBBy",
       new Font("Segoe Print", 22, FontStyle.Bold),
       SystemBrushes.WindowText, new Point(700, 530));
    Response.ContentType = "image/jpeg";
    var encoderParameters = new EncoderParameters(1);
    encoderParameters.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 1L);
    string uriPath = "http://www.tufanyumlu.com/images/deneme.jpg";
    string localPath = new Uri(uriPath).LocalPath;
    bitMapImage.Save(localPath, GetEncoder(ImageFormat.Jpeg), encoderParameters);
}
private static ImageCodecInfo GetEncoder(ImageFormat format)
{
    var codecs = ImageCodecInfo.GetImageDecoders();
    foreach (var codec in codecs)
    {
        if (codec.FormatID == format.Guid)
        {
            return codec;
        }
    }
    return null;
}
1

1 Answers

0
votes

Your exception (Generic error occurred in gdi+ in english) is caused by invalid path in BitMap.Save-method call or missing access rights. In your case, path you was trying to save is "/images/deneme.jpg".

You should set saving path explicitly in your code. You can add image saving root folder in your application's config-file. So last lines in your Page_Load-method could be i.e:

var root = ConfigurationSettings.AppSettings["downloadRootPath"];
string localPath = root + new Uri(uriPath).LocalPath;
bitMapImage.Save(localPath, GetEncoder(ImageFormat.Jpeg), encoderParameters);

..while you have following configuration in your app.config or web.config depending on run-time context:

<appSettings>
  <add key="downloadRootPath" value="C:\TEMP\"/>
</appSettings>

Also be sure that folder exists and your application has proper access rights.