---
title: "Problem med bilduppladdningsklass"
type: "forum-thread"
url: "https://www.webforum.nu/amne/dotnet/172470-problem-med-bilduppladdningsklass"
topic: ".NET"
topic_url: "https://www.webforum.nu/amne/dotnet"
author: "SebastianE"
published: "2008-06-29T15:24:04.000Z"
updated: "2008-06-30T06:40:32.000Z"
replies: 1
views: 357
page: 1
pages: 1
language: "sv-SE"
site: "webForum — webforum.nu"
rights: "Upphovsrätten till varje inlägg tillhör dess författare."
attribution: "Citera som: webForum, https://www.webforum.nu/amne/dotnet/172470-problem-med-bilduppladdningsklass"
---

# Problem med bilduppladdningsklass

## #1 — SebastianE, 2008-06-29T15:24Z

Hej
Har problem med att ladda upp bilder på min hemsida. Får det klassiska felet 
" System.Runtime.InteropServices.ExternalException: A generic error occurred in GDI+." när jag försöker ladda upp bilden. Har kollat runt lite vad det beror och oftast var därför man inte hade några skrivrättigheter till en mapp på servern, men det har jag fixat. Får det ändå inte att fungera.

Dock får jag min klass att fungera på localhost.

Någon som kan se vad jag gör för fel?

```
using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
using System.IO;

/// <summary>
/// Summary description for UploadPicture
/// </summary>
public class UploadPicture
{
	public void Picture(FileUpload uploadPicture, int width, string type, string catalog, string saveFileName, int quality)
	{
        if(uploadPicture.HasFile)
        {
            string fileName = HttpContext.Current.Server.HtmlEncode(uploadPicture.FileName);
            string fileExt = System.IO.Path.GetExtension(fileName).ToLower();
            string mimetype = uploadPicture.PostedFile.ContentType;

            if(fileExt==".jpg" || fileExt==".jpeg" || fileExt==".png" || fileExt==".gif"
            || mimetype == "image/jpeg" || mimetype == "image/png" || mimetype == "image/gif")
            {
                try
                {
                    //For big picture
                    if(type=="big")
                    {
                        System.Drawing.Image picture = System.Drawing.Bitmap.FromStream(uploadPicture.PostedFile.InputStream);
                        
                        int bigHeight, bigWidth;
                        bigHeight = picture.Height;
                        bigWidth = picture.Width;

                        //Set the maxwidth
                        if(bigWidth > width)
                        {
                            double tmpWidth = Convert.ToDouble(width);
                            double tmpBigWidth = Convert.ToDouble(bigWidth);
                            double tmpBigHeight = Convert.ToDouble(bigHeight);

                            bigHeight = Convert.ToInt32((tmpWidth / tmpBigWidth) * tmpBigHeight);
                            bigWidth = width;
                        }

                        // Resize the image
                        picture = resizeImage(picture, bigWidth, bigHeight);
                        
                        //Save the picture
                        string webFileName = System.IO.Path.GetFileName(fileName);
                        webFileName = HttpContext.Current.Server.MapPath(catalog + "/") + saveFileName + ".jpg";
                        SaveJPGWithCompressionSetting(picture, @webFileName, quality);
                        picture.Dispose();
                    }

                    else if (type == "thumb")
                    {
                        //Create a thumbnails
                        System.Drawing.Image thumbImage = System.Drawing.Image.FromStream(uploadPicture.PostedFile.InputStream);

                        int heightThumb, widthThumb;
                        heightThumb = thumbImage.Height;
                        widthThumb = thumbImage.Width;

                        //Set the maxwidth
                        if (widthThumb > width)
                        {
                            widthThumb = width;
                            heightThumb = heightThumb/(thumbImage.Height/width);
                        }

                        // Resize the image
                        thumbImage = resizeImage(thumbImage, widthThumb, heightThumb);

                        //Save the picture
                        string webFileName = System.IO.Path.GetFileName(fileName);
                        webFileName = HttpContext.Current.Server.MapPath(catalog + "/") + saveFileName +".jpg";
                        SaveJPGWithCompressionSetting(thumbImage, @webFileName, 50);

                        thumbImage.Dispose();
                    }
                    else
                        throw new Exception("Felaktig metod. Giltiga metoder är big och thumbs.");
                }
                catch (Exception ex)
                {
                    throw new Exception("Problem med att ladda upp bild.<br />"+ex.Message);
                }
            }
            else
            {
                throw new Exception("Felaktigt filformat eller Mimetype.");
            }
        }
	}

    //Compress the file-size
    private System.Drawing.Image resizeImage(System.Drawing.Image img, int width, int height)
    {
        System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(width, height);
        System.Drawing.Graphics g = System.Drawing.Graphics.FromImage((System.Drawing.Image)bitmap);
        g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
        g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
        g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;

        g.DrawImage(img, 0, 0, width, height);
        g.Dispose();

        return (System.Drawing.Image)bitmap;
    }

    private void SaveJPGWithCompressionSetting(System.Drawing.Image image, string szFileName, long lCompression)
    {
        System.Drawing.Imaging.EncoderParameters eps = new System.Drawing.Imaging.EncoderParameters(1);
        eps.Param[0] = new System.Drawing.Imaging.EncoderParameter(System.Drawing.Imaging.Encoder.Quality, lCompression);
        System.Drawing.Imaging.ImageCodecInfo ici = GetEncoderInfo("image/jpeg");
        image.Save(szFileName, ici, eps);
    }

    private static System.Drawing.Imaging.ImageCodecInfo GetEncoderInfo(String mimeType)
    {
        int j;
        System.Drawing.Imaging.ImageCodecInfo[] encoders;
        encoders = System.Drawing.Imaging.ImageCodecInfo.GetImageEncoders();
        for (j = 0; j < encoders.Length; ++j)
        {
            if (encoders[j].MimeType == mimeType)
                return encoders[j];
        }
        return null;
    }
 
}
```

Permalänk: https://www.webforum.nu/p/172470

## #2 — Gladh, 2008-06-30T06:40Z

> **SebastianE skrev:**
>
> Hej
> Har problem med att ladda upp bilder på min hemsida. Får det klassiska felet 
> " System.Runtime.InteropServices.ExternalException: A generic error occurred in GDI+." när jag försöker ladda upp bilden. Har kollat runt lite vad det beror och oftast var därför man inte hade några skrivrättigheter till en mapp på servern, men det har jag fixat. Får det ändå inte att fungera.
> 
> Dock får jag min klass att fungera på localhost.

Då det fungerar på din maskin, men inte på servern så lutar det fortfarande åt ett rättighetesproblem. Så frågan är hur fixade du det med rättigheterna? För ASP.NET körs på 2 konto, och om du inte har gjort impersonat på den användaren som du är inloggad som så kommer (har jag för mig) du köra med rättigheter som din IWAM användare, eller vem det är som kör har rättigheter för din process.

Så vad gjorde du för att fixa rättigheterna?

\- M

Permalänk: https://www.webforum.nu/p/2120763

---

Tråden på webben: https://www.webforum.nu/amne/dotnet/172470-problem-med-bilduppladdningsklass
