Page 1 of 1

Visual C# Windows Forms PictureBox Clipping and Color Filter

Posted: Tue May 08, 2012 6:00 pm
by OmenFelix
Hey guys I was wondering if I had a sprite-sheet and I wanted to load only a certain 32x32 region of it into a 'PictureBox' of a Windows Form, how would I go about doing that? Also as a bonus, how would I filter the Magenta(255,0,255) and render as alpha, like SDL colour-keying. NOTE: I'm not too bothered about the second question, as that can be done somewhat manually by creating a magenta to alpha program.

Thanks in advance. ;)

Re: Visual C# Windows Forms PictureBox Clipping and Color Fi

Posted: Tue May 08, 2012 10:12 pm
by dandymcgee
A better questions is why would you want to do that? I hope this is an editor question..

Re: Visual C# Windows Forms PictureBox Clipping and Color Fi

Posted: Wed May 09, 2012 2:07 am
by OmenFelix
dandymcgee wrote:A better questions is why would you want to do that? I hope this is an editor question..
Yes it is. It's only a temporary measure as none of the team really have experience with editors nor want to do much about it. Just to test the tiling system really.

Re: Visual C# Windows Forms PictureBox Clipping and Color Fi

Posted: Wed May 09, 2012 3:05 am
by tappatekie
OmenFelix wrote:Hey guys I was wondering if I had a sprite-sheet and I wanted to load only a certain 32x32 region of it into a 'PictureBox' of a Windows Form, how would I go about doing that? Also as a bonus, how would I filter the Magenta(255,0,255) and render as alpha, like SDL colour-keying. NOTE: I'm not too bothered about the second question, as that can be done somewhat manually by creating a magenta to alpha program.

Thanks in advance. ;)
You could use cropping?

Code: Select all

Bitmap bmp = new Bitmap(32,32);
//Draw the spritesheet bitmap
Graphics.DrawImage(spritesheet, new Rectangle(-spiteX, -spriteY, 32, 32));
//Set the picture box image and size
pictureBox.Image = bmp;
pictureBox.Size = bmp.Size;
spriteX and spriteY is negative since we want to offset the render of the main sprite image and select 32x32 pixels of it.
It'l do the job...

This however may not be the best way to go about this since I'm not sure than rendering the spritesheet for every sprite is as-efficent.

You could also use

Code: Select all

Bitmap mySprite = (Bitmap)spritesheet.Clone(new Rectangle(spriteX, spriteY, 32,32), PixelFormat.Gdi);

Re: Visual C# Windows Forms PictureBox Clipping and Color Fi

Posted: Wed May 09, 2012 10:34 am
by OmenFelix
tappatekie wrote:
OmenFelix wrote:Hey guys I was wondering if I had a sprite-sheet and I wanted to load only a certain 32x32 region of it into a 'PictureBox' of a Windows Form, how would I go about doing that? Also as a bonus, how would I filter the Magenta(255,0,255) and render as alpha, like SDL colour-keying. NOTE: I'm not too bothered about the second question, as that can be done somewhat manually by creating a magenta to alpha program.

Thanks in advance. ;)
You could use cropping?

Code: Select all

Bitmap bmp = new Bitmap(32,32);
//Draw the spritesheet bitmap
Graphics.DrawImage(spritesheet, new Rectangle(-spiteX, -spriteY, 32, 32));
//Set the picture box image and size
pictureBox.Image = bmp;
pictureBox.Size = bmp.Size;
spriteX and spriteY is negative since we want to offset the render of the main sprite image and select 32x32 pixels of it.
It'l do the job...

This however may not be the best way to go about this since I'm not sure than rendering the spritesheet for every sprite is as-efficent.

You could also use

Code: Select all

Bitmap mySprite = (Bitmap)spritesheet.Clone(new Rectangle(spriteX, spriteY, 32,32), PixelFormat.Gdi);
This code isn't for a PictureBox is it?

Re: Visual C# Windows Forms PictureBox Clipping and Color Fi

Posted: Wed May 09, 2012 11:33 am
by tappatekie
OmenFelix wrote:
tappatekie wrote:
OmenFelix wrote:Hey guys I was wondering if I had a sprite-sheet and I wanted to load only a certain 32x32 region of it into a 'PictureBox' of a Windows Form, how would I go about doing that? Also as a bonus, how would I filter the Magenta(255,0,255) and render as alpha, like SDL colour-keying. NOTE: I'm not too bothered about the second question, as that can be done somewhat manually by creating a magenta to alpha program.

Thanks in advance. ;)
You could use cropping?

Code: Select all

Bitmap bmp = new Bitmap(32,32);
//Draw the spritesheet bitmap
Graphics.DrawImage(spritesheet, new Rectangle(-spiteX, -spriteY, 32, 32));
//Set the picture box image and size
pictureBox.Image = bmp;
pictureBox.Size = bmp.Size;
spriteX and spriteY is negative since we want to offset the render of the main sprite image and select 32x32 pixels of it.
It'l do the job...

This however may not be the best way to go about this since I'm not sure than rendering the spritesheet for every sprite is as-efficent.

You could also use

Code: Select all

Bitmap mySprite = (Bitmap)spritesheet.Clone(new Rectangle(spriteX, spriteY, 32,32), PixelFormat.Gdi);
This code isn't for a PictureBox is it?
You can apply the code to a picture box... with "pictureBox.Image = croppedBitmap;", the cropped bitmap would be the mySprite bit in the code...

Unless you want the picturebox to only show a certain part of the image (spritesheet) with the picture box image property set to the spritesheet image, if so, please be more specific, you said to "load only a certain 32x32 region of it into a 'PictureBox' " ("INTO"...)

Re: Visual C# Windows Forms PictureBox Clipping and Color Fi

Posted: Thu May 10, 2012 12:17 pm
by OmenFelix
tappatekie wrote:
OmenFelix wrote:
tappatekie wrote:
OmenFelix wrote:Hey guys I was wondering if I had a sprite-sheet and I wanted to load only a certain 32x32 region of it into a 'PictureBox' of a Windows Form, how would I go about doing that? Also as a bonus, how would I filter the Magenta(255,0,255) and render as alpha, like SDL colour-keying. NOTE: I'm not too bothered about the second question, as that can be done somewhat manually by creating a magenta to alpha program.

Thanks in advance. ;)
You could use cropping?

Code: Select all

Bitmap bmp = new Bitmap(32,32);
//Draw the spritesheet bitmap
Graphics.DrawImage(spritesheet, new Rectangle(-spiteX, -spriteY, 32, 32));
//Set the picture box image and size
pictureBox.Image = bmp;
pictureBox.Size = bmp.Size;
spriteX and spriteY is negative since we want to offset the render of the main sprite image and select 32x32 pixels of it.
It'l do the job...

This however may not be the best way to go about this since I'm not sure than rendering the spritesheet for every sprite is as-efficent.

You could also use

Code: Select all

Bitmap mySprite = (Bitmap)spritesheet.Clone(new Rectangle(spriteX, spriteY, 32,32), PixelFormat.Gdi);
This code isn't for a PictureBox is it?
You can apply the code to a picture box... with "pictureBox.Image = croppedBitmap;", the cropped bitmap would be the mySprite bit in the code...

Unless you want the picturebox to only show a certain part of the image (spritesheet) with the picture box image property set to the spritesheet image, if so, please be more specific, you said to "load only a certain 32x32 region of it into a 'PictureBox' " ("INTO"...)
Thanks! I'll try it later. :3