Notifications
Clear all

[Closed] Is there a quicker way to colour a dot Net Bitmap?

Hi,

I’m trying to write a function that takes a maxscript color and returns a small dot Net bitmap of that colour.

The only way I can find that works is to use setPixel to set each pixel individually:

fn getColChip col =
(
	local dnCol = (dotNetClass "System.Drawing.Color").fromARGB col.r col.g col.b
	local colImage = dotnetObject "System.Drawing.Bitmap" 15 15
	for x = 0 to 14 do
	(
		for y = 0 to 14 do
		(
			colImage.setPixel x y dnCol
		)
	)
	colImage
)

Surely there is a faster way that this?

Thanks.

5 Replies

15 x 15 is pretty small so it shouldn’t be very slow unless you are doing this a LOT!
it might be faster to use something like Graphics.FromImage and then Graphics.FillRectangle
if you are doing this a lot and the bitmaps are all the same size then i’d suggest using the Graphics method with a source bitmap, fill with your required colour and use Bitmap.Clone() to get the new bitmap.

 lo1

What Gravey said, but instead of Graphics.FillRectangle, use Graphics.Clear(color)

Thanks for the replies. So my function would look something like:

fn getColChip col =
(
	local colImage = dotnetObject "System.Drawing.Bitmap" 15 15
	local colGraphic = ((dotNetClass "System.Drawing.Graphics").FromImage colImage)
	colGraphic.clear ((dotNetClass "System.Drawing.Color").fromARGB col.r col.g col.b)
	--how do I convert colGraphic to a Bitmap?
)

How do I convert the graphic back to a bitmap?

Thanks again for your help.

 lo1

You don’t, the graphic painted directly to the bitmap, it should already be there.
What you should do is dispose the graphics object after you’re done painting.

:lightbulb Right I that works. Thanks once again for your help lo.