C
C#10mo 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
TsjechOP10mo ago
BoxSize is nothing crazy, its set to 100
br4kejet
br4kejet10mo 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
TsjechOP10mo 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