Skip to content

Instantly share code, notes, and snippets.

@diogotito
Created September 2, 2025 17:20
Show Gist options
  • Select an option

  • Save diogotito/d876edd6f37b017a309229cbca2cb2b9 to your computer and use it in GitHub Desktop.

Select an option

Save diogotito/d876edd6f37b017a309229cbca2cb2b9 to your computer and use it in GitHub Desktop.
A CodeLab BitmapEffect that draws a grid in Paint.NET
// Name: Grid
// Submenu: AAAAAAAA
// Author: Diogo Marques
// Title: Grid
// Version: 0.01
// Desc: Renders a grid
// Keywords:
// URL:
// Help:
// For help writing a Bitmap plugin: https://boltbait.com/pdn/CodeLab/help/tutorial/bitmap/
#region UICode
IntSliderControl GridSize = 8; // [0,256] Grid Size
PanSliderControl Offset = new Vector2Double(0.000, 0.000); // Offset
ColorWheelControl ManagedGridColor = ColorWheelControl.Create(SrgbColors.Gray); // [Gray] Color
#endregion
protected override void OnRender(IBitmapEffectOutput output)
{
using IEffectInputBitmap<ColorBgra32> sourceBitmap = Environment.GetSourceBitmapBgra32();
using IBitmapLock<ColorBgra32> sourceLock = sourceBitmap.Lock(new RectInt32(0, 0, sourceBitmap.Size));
RegionPtr<ColorBgra32> sourceRegion = sourceLock.AsRegionPtr();
RectInt32 outputBounds = output.Bounds;
using IBitmapLock<ColorBgra32> outputLock = output.LockBgra32();
RegionPtr<ColorBgra32> outputSubRegion = outputLock.AsRegionPtr();
var outputRegion = outputSubRegion.OffsetView(-outputBounds.Location);
// Delete any of these lines you don't need
//ColorBgra32 primaryColor = Environment.PrimaryColor.GetBgra32(sourceBitmap.ColorContext);
//ColorBgra32 secondaryColor = Environment.SecondaryColor.GetBgra32(sourceBitmap.ColorContext);
// Make Offset relative to Grid Size
Vector2Int32 ScaledOffset = Vector2Int32.Round(Offset * GridSize);
// Get color for grid
//PaintDotNet.Imaging.ManagedColorExtensions.GetBgra32(ManagedGridColor, sourceBitmap.ColorContext);
//ManagedGridColor.GetBgra32(sourceBitmap.ColorContext);
ColorBgra GridColor = ManagedGridColor.GetBgra32(sourceBitmap.ColorContext);
// Loop through the output canvas tile
for (int y = outputBounds.Top; y < outputBounds.Bottom; ++y)
{
if (IsCancelRequested) return;
for (int x = outputBounds.Left; x < outputBounds.Right; ++x)
{
ColorBgra32 sourcePixel = sourceRegion[x,y];
int ox = (int)ScaledOffset.X; if (ox < 0) ox += GridSize;
int oy = (int)ScaledOffset.Y; if (oy < 0) oy += GridSize;
bool isGrid = x % GridSize == ox ||
y % GridSize == oy;
outputRegion[x,y] = isGrid ? /*GridBlendOp.Apply(sourcePixel, GridColor)*/ GridColor : sourcePixel;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment