---
title: "ändra storlek på bild"
type: "forum-thread"
url: "https://www.webforum.nu/amne/dotnet/167331-ändra-storlek-på-bild"
topic: ".NET"
topic_url: "https://www.webforum.nu/amne/dotnet"
author: "Vinnaren"
published: "2007-12-16T22:54:28.000Z"
updated: "2007-12-17T15:50:06.000Z"
replies: 5
views: 545
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/167331-ändra-storlek-på-bild"
---

# ändra storlek på bild

## #1 — Vinnaren, 2007-12-16T22:54Z

Håller på med en bilduppladdning, där bilden som laddas upp ska vara/bli 780px bred.

Har följande hittills:

```
            String extension = Path.GetExtension(fuBanner.FileName).ToLower();

            String filename = Guid.NewGuid() + extension;
            int height, width;

            fuBanner.SaveAs(Server.MapPath("upload/temp") + filename);

            System.Drawing.Image img = Bitmap.FromStream(fuBanner.PostedFile.InputStream);

            height = img.Height;
            width = img.Width;
```

 

Hur gör jag om bilden till 780px?

Tacksam för all hjälp   :stud

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

## #2 — Coercri, 2007-12-17T05:47Z

Att söka på webforum hjälper ibland. <http://www.webforum.nu/showthread.php?t=145438> kan nog hjälpa dig.

Det du måste göra är att läsa in filen i minnet och arbeta med den i en memorystream för att sedan skriva den till din uploadpath.

Själv föredrar jag att sktiva filer till databasen för att det ger mig mer kontroll och mindre strul med behörigheter i filsystemet.

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

## #3 — Gladh, 2007-12-17T07:25Z

> **coercri skrev:**
>
> Själv föredrar jag att sktiva filer till databasen för att det ger mig mer kontroll och mindre strul med behörigheter i filsystemet.

Om man väljer detta alternativ (som har sina fördelar/nackdelar) så **måste** man se till så att bilderna läggs i en lokal cache när de är omvandlade från sin dataström till ett bildobjekt. Eftersom prestandan blir rejält lidande när man använder GDI+ för att skapa bilder från en dataström. 

\- M

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

## #4 — Vinnaren, 2007-12-17T09:43Z

ok, men löste det med:

```
img = img.GetThumbnailImage(780, height, null, System.IntPtr.Zero);
```

 

Kanske inte det bästa sättet men det var smidigt  :i

Eller kanske finns nott bättre sätt  :q

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

## #5 — Vinnaren, 2007-12-17T15:12Z

får ett litet felmeddelande när jag kör uppladdningen:
 An object reference is required for the nonstatic field, method, or property 'System.Web.UI.Page.Server.get'

på:

```
fu.SaveAs(Server.MapPath("~/upload/temp/") + filename);
```

 

Hur löser man detta?

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

## #6 — Zaiman, 2007-12-17T15:50Z

Saxat från internet:

```
public static Image FixedSize(Image imgPhoto, int Width, int Height)
        {
            int sourceWidth = imgPhoto.Width;
            int sourceHeight = imgPhoto.Height;
            int sourceX = 0;
            int sourceY = 0;
            int destX = 0;
            int destY = 0;

            float nPercent = 0;
            float nPercentW = 0;
            float nPercentH = 0;

            nPercentW = ((float)Width / (float)sourceWidth);
            nPercentH = ((float)Height / (float)sourceHeight);

            //if we have to pad the height pad both the top and the bottom
            //with the difference between the scaled height and the desired height
            if (nPercentH < nPercentW)
            {
                nPercent = nPercentH;
                destX = (int)((Width - (sourceWidth * nPercent)) / 2);
            }
            else
            {
                nPercent = nPercentW;
                destY = (int)((Height - (sourceHeight * nPercent)) / 2);
            }

            int destWidth = (int)(sourceWidth * nPercent);
            int destHeight = (int)(sourceHeight * nPercent);

            Bitmap bmPhoto = new Bitmap(Width, Height, PixelFormat.Format24bppRgb);
            bmPhoto.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution);

            Graphics grPhoto = Graphics.FromImage(bmPhoto);
            grPhoto.Clear(Color.White);
            grPhoto.InterpolationMode = InterpolationMode.HighQualityBicubic;

            grPhoto.DrawImage(imgPhoto,
                new Rectangle(destX, destY, destWidth, destHeight),
                new Rectangle(sourceX, sourceY, sourceWidth, sourceHeight),
                GraphicsUnit.Pixel);

            grPhoto.Dispose();
            return bmPhoto;
        }
```

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

---

Tråden på webben: https://www.webforum.nu/amne/dotnet/167331-ändra-storlek-på-bild
