C
C#4mo ago
Tsjech

Bitmap PixelFormat causing OutOfMemoryException

When I add a size to Bitmap bmp setting the label.Image throws an OutOfMemoryException, but not when I remove the size to Bitmap bmp Does anyone know what I have to do to fix this?
private void PromotePiece(Move move)
{
Bitmap bmp = new Bitmap(PiecesImage, boxSize, boxSize);

if (move.PromotedPiece is Knight) {
Label promotionKnigt = new Label();
promotionKnigt.Location = new Point(move.Location.X * boxSize + boxSize / 2, move.Location.Y * boxSize);
promotionKnigt.Size = new Size(boxSize, boxSize);
promotionKnigt.Image = bmp.Clone(move.PromotedPiece.PieceOfImage, bmp.PixelFormat);
promotionKnigt.Click += PromoteToKnight;
this.Controls.Add(promotionKnigt);
} else {
Label promotionQueen = new Label();
promotionQueen.Location = new Point(move.Location.X * boxSize + boxSize / 2, (move.Location.Y + 1) * boxSize);
promotionQueen.Size = new Size(boxSize, boxSize);
promotionQueen.Image = bmp.Clone(move.PromotedPiece.PieceOfImage, bmp.PixelFormat);
promotionQueen.Click += PromoteToQueen;
this.Controls.Add(promotionQueen);
}
}
private void PromotePiece(Move move)
{
Bitmap bmp = new Bitmap(PiecesImage, boxSize, boxSize);

if (move.PromotedPiece is Knight) {
Label promotionKnigt = new Label();
promotionKnigt.Location = new Point(move.Location.X * boxSize + boxSize / 2, move.Location.Y * boxSize);
promotionKnigt.Size = new Size(boxSize, boxSize);
promotionKnigt.Image = bmp.Clone(move.PromotedPiece.PieceOfImage, bmp.PixelFormat);
promotionKnigt.Click += PromoteToKnight;
this.Controls.Add(promotionKnigt);
} else {
Label promotionQueen = new Label();
promotionQueen.Location = new Point(move.Location.X * boxSize + boxSize / 2, (move.Location.Y + 1) * boxSize);
promotionQueen.Size = new Size(boxSize, boxSize);
promotionQueen.Image = bmp.Clone(move.PromotedPiece.PieceOfImage, bmp.PixelFormat);
promotionQueen.Click += PromoteToQueen;
this.Controls.Add(promotionQueen);
}
}
3 Replies
Tsjech
Tsjech4mo ago
BoxSize is nothing crazy, its set to 100
br4kejet
br4kejet4mo ago
Which line does the exception happen on? Looking at the source code for Bitmap and Graphics they don't seem to ever throw OutOfMemoryException directory so I guess your system doesn't have enough memory to store the image? Either that or the runtime is memory limited or something like that
Tsjech
Tsjech4mo ago
promotionQueen.Image = bmp.Clone(move.PromotedPiece.PieceOfImage, bmp.PixelFormat); throws the exception, I have accessed the image at other places in my code and everything works fine. Without adding the Size to the decleration of bmp this code runs fine too, the image is just too big for the label
Want results from more Discord servers?
Add your server
More Posts
Is there a list of all .NET Core gotchas? Can you list some...I accidentally referenced a .NET Framework (4.5) dll in the csproj of a .NET 6 application. No warniHandling events in "blocks"Hi, I am dealing with a problem where I need to move an Elevator based on the inputs received. The Blazor Server App. Error KeyNotFoundException: The given key was not present in the dictionary.Hello i tried to connect my Blazor Server App to my postgresSQL, i already installed PostgresSQL andCan these ternary expression with expensive method calls be simplified without outline variables ...I do **NOT** want to - call expensive methods more than once. - make "outline" temporary variables✅ Can't modify a list outside of the cycleSo I'm trying to modify differenceList in a cycle, but it gives me an error (see attachment), althouClass Naming ConventionsI'm currently renaming all the classes in my Discord Bot project but I'm stumped to decide what the ✅ [AVALONIA] Get all coordinates along a PolylineWhilst you define the points for a Polyline, is there any way that I can get or calculate the rest oCleaner NavigationView implementation (Avalonia)I'm currently porting an existing project that i didn't write from WinUI3 to Avalonia. The project Iprogram closing early```cs Console.WriteLine("What was the digital portfolio mark (out of 35)?"); int dpMark = int.Parse(Fully stuck on making a clickable game object system in unityim fully stuck right now, I cant find a good solution for this problem, so basically im making a gam