webForumDet fria alternativet

Beskära bild

.NET

2 svar · 313 visningar · startad av doggelito

Medlem sedan juni 20003 076 inlägg
Frågan#1

Ja, går det i .NET? :q

Jag har kikat lite på SetClip under System.Drawing.Graphics.
Med SetClip så får jag ut rektangeln för den nya bilden, men hur sparar jag sedan denna rektangel till en ny bild?
Men man kanske inte ens ska använda SetClip för att beskära bilder! :stud

Medlem sedan juni 20008 205 inlägg
#2

Vet inte om det finns något effektivare sätt att göra't på, men följande borde väl funka?

Image orig = ...;
Graphics g = Graphics.FromImage(orig);
Rectangle crop = ...;
g.SetClip(crop);
Image cropped = new Bitmap(crop.Width, crop.Height, g);
Medlem sedan juni 20003 076 inlägg
#3

Hajjar inte riktigt hur Clip ska funka, men, men, jag löste det så här: :)

//workImage = Orginalbilden

public void Clip(int Top, int Right, int Bottom, int Left)
        {

            Rectangle destRect = new Rectangle(0, 0, (workImage.Width - Left - Right), (workImage.Height - Top - Bottom));
               
            Bitmap bitmap = new Bitmap(workImage.Width-Left-Right, workImage.Height-Top-Bottom);
            Graphics graphics = Graphics.FromImage(bitmap);

            graphics.DrawImage(workImage, destRect, Left, Top, workImage.Width-Left-Right, workImage.Height-Top-Bottom, GraphicsUnit.Pixel);

            workImage = bitmap;
        }
266 ms totalt · 4 externa anrop · v20260731065814-full.1dc6f849
124 ms — deklarationer (db)
0 ms — hämta statistik (cache)
135 ms — hämta tråd, inlägg och bilagor (db)
128 ms — ändringar (db)