I am working on a circular menu application.
Before I use a bitmap as background, I must crop it as a circle.
Well, I found a code snippet for cropping the picture C# crop circle in a image or bitmap or:
public System.Drawing.Image x(string sourceFile, int circleUpperLeftX, int circleUpperLeftY, int circleDiameter)
{
Bitmap SourceImage = new Bitmap(System.Drawing.Image.FromFile(sourceFile));
Rectangle CropRect = new Rectangle(circleUpperLeftX, circleUpperLeftY, circleDiameter, circleDiameter);
Bitmap CroppedImage = SourceImage.Clone(CropRect, SourceImage.PixelFormat);
TextureBrush TB = new TextureBrush(CroppedImage);
Bitmap FinalImage = new Bitmap(circleDiameter, circleDiameter);
Graphics G = Graphics.FromImage(FinalImage);
G.FillEllipse(TB, 0, 0, circleDiameter, circleDiameter);
return FinalImage;
}
But the code causes memory leak after a while in line 6.
Well, I tried adding TB.Dispose(); to prevent it but, that didn’t help.
What should I do?
Well, Solved It.
As you said, textbrush is not causing the memory leak.
I used to use this code:
When I used this, I mean Disposing PictureBox’s Image before assigning a new Bitmap to it prevented the memory leak.
Thank you all for your help!