Jump to content

Sample C# code plugin to load an image

John Scott

Recommended Posts

I have a custom image format that I've had a Paint.NET plugin to view for about 4 years now. Unfortunately, with moving to Net5, it no longer compiles. Is there source for a sample plugin using the latest and greatest to load an image out there?


Alternatively, given and int32[] of RGBA pixels, how would I convert that to a BitmapLayer (to add to a document)? 

I'm sorry to ask something so simple, but I'm having no luck with my google-fu



Link to comment
Share on other sites

DirectBitmap is in the StackOverflow link. Even older code is commented out.



private void AddLayer( Document mippedDocument, int8 mip, int32[] image, FGameTexHeader header, int32 widthInBlocks, int32 heightInBlocks )
	// Copy the data to a bitmap
	int32 width = widthInBlocks * header.BlockWidth;
	int32 height = heightInBlocks * header.BlockHeight;

	// https://stackoverflow.com/questions/24701703/c-sharp-faster-alternatives-to-setpixel-and-getpixel-for-bitmaps-for-windows-f
	DirectBitmap bitmap = new DirectBitmap(header.MaxWidth, header.MaxHeight);
	// 	Bitmap bitmap = new Bitmap( header.MaxWidth, header.MaxHeight, PixelFormat.Format32bppArgb );

	for ( int32 y = 0; y < height; y++ )
		for (int32 col = 0; col < width; ++col)
			bitmap.SetPixel(col, y, Color.FromArgb(image[y * width + col]));

		// Rectangle area = new Rectangle( 0, y, width, 1 );
		// BitmapData pixel_data = bitmap.LockBits( area, ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb );
		// Marshal.Copy( image, y * width, pixel_data.Scan0, width );
		// bitmap.UnlockBits( pixel_data );

	// Copy the bitmap to a Paint.NET layer
	Surface mip_surface = Surface.CopyFromBitmap(bitmap.GetBitmap(), false );
	// Surface mip_surface = Surface.CopyFromBitmap(bitmap, false);
	BitmapLayer mip_layer = new BitmapLayer( mip_surface );
	mip_layer.Name = "Mip " + mip;
	mip_layer.Visible = ( mip == 0 );
	mip_layer.BlendMode = LayerBlendMode.Normal;
	mip_layer.Opacity = 255;
	mippedDocument.Layers.Add( mip_layer );



Link to comment
Share on other sites

I wouldn't create an intermediate Bitmap. Just create a new BitmapLayer and set the pixels in a loop

            var layer = new BitmapLayer(pixelWidth, pixelHeight);
            layer.Surface[column, row] = ...

We just discussed what may be the fastest way to set the surface data from a raster.



  • Upvote 1

midoras signature.gif

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...