Paint Net custom Watermark effect with CodeLab

How to make custom Watermark effect in  Paint Net with CodeLab 

 

intro How to make custom Watermark effect in  Paint Net with CodeLab

In this tutorial, we will create a custom watermark plugin for Paint Net  using the CodeLab tool.

Pre-conditions :

The Paint Net application is already installed on your personal computer.

Solving : 

[watch on YouTube ] 

Step 1 - Download the CodeLab plugin for Paint Net.

Download the CodeLab plugin from the website shown in the image. It is a free development environment for writing Paint Net effect plugins in C Sharp.

Open CodeLab official page

The download link is located in the middle of the page, as shown in the image below.


Step 2 - Unzip CodeLab archive.

Locate downloaded CodeLab zip archive.

Locate downloaded CodeLab zip archive

Unzip CodeLab archive.

Unzip CodeLab archive

Step 3 - Install CodeLab plugin

Open unzipped folder of CodeLab plugin.

Open unzipped folder of CodeLab plugin

Run CodeLab installer file.

Run CodeLab installer file

In the CodeLab Installer wizard, enable the license agreement checkbox. It would be nice if you read it, of course.

enable the license agreement checkbox

Click the Install button in the bottom-right corner of the CodeLab installation wizard.

Click the Install button in the bottom-right corner of the CodeLab installation wizard

Wait until the installation is finished. Click the OK button in the dialog.

Wait until the installation is finished. Click the OK button in the dialog.

Click the Close button.

Click the Close button


Step 4 - open CodeLab plugin in Paint Net.

Launch Paint Net. In the menu bar, click Effects, then select Advanced, and finally click CodeLab.

open CodeLab plugin in Paint Net

You will see the CodeLab Editor. In my case, it is CodeLab version 6.13. 

You will see the CodeLab Editor.

Step 5 - Implement the custom Watermark effect code.

In the CodeLab Editor, enable the Show output errors checkbox in the bottom-left corner.

In the CodeLab Editor, enable the Show output errors checkbox in the bottom-left corner

In CodeLab Editor, click on the File menu item, then click on the New menu item, then click on the Bitmap Effect menu item.

In CodeLab Editor, click on the File menu item, then click on the New menu item, then click on the Bitmap Effect menu item.


Prepare watermark or logo image, name it PAINT_NET_LOGO.png and place it into Downloads folder on your Windows system.

Prepare watermark or logo image, name it PAINT_NET_LOGO.png and place it into Downloads folder on your Windows system.


Copy and paste the code from the text version of the tutorial into the CodeLab editor. Please find the link to the text version in the video description.

Copy and paste the code from the text version of the tutorial into the CodeLab editor. Please find the link to the text version in the video description.

// Name: Watermark Logo (CPU)

// Submenu: Render

// Author: Custom

// Title: Watermark Logo

#region UICode

IntSliderControl Amount1 = 20;  // [0,500] Margin

IntSliderControl Amount2 = 100; // [10,500] Scale %

IntSliderControl Amount3 = 100; // [0,100] Opacity %

#endregion


private Surface logoSurface = null;


protected override void OnRender(IBitmapEffectOutput output)

{

    // 1. Load the logo once

    if (logoSurface == null)

    {

        string downloadsPath = System.IO.Path.Combine(

            System.Environment.GetFolderPath(System.Environment.SpecialFolder.UserProfile),

            "Downloads", "PAINT_NET_LOGO.png"

        );


        if (System.IO.File.Exists(downloadsPath))

        {

            try {

                using (var sysBmp = new System.Drawing.Bitmap(downloadsPath))

                {

                    logoSurface = Surface.CopyFromBitmap(sysBmp);

                }

            } catch { /* Handle error */ }

        }

    }


    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);


    // Calculate dimensions

    float scale = Amount2 / 100f;

    int logoW = (logoSurface != null) ? (int)(logoSurface.Width * scale) : 0;

    int logoH = (logoSurface != null) ? (int)(logoSurface.Height * scale) : 0;

    int startX = Environment.Document.Size.Width - logoW - Amount1;

    int startY = Environment.Document.Size.Height - logoH - Amount1;

    float opacity = Amount3 / 100f;


    for (int y = outputBounds.Top; y < outputBounds.Bottom; ++y)

    {

        if (IsCancelRequested) return;


        for (int x = outputBounds.Left; x < outputBounds.Right; ++x)

        {

            ColorBgra32 bg = sourceRegion[x, y];

            

            // Check if current pixel is within the watermark area

            if (logoSurface != null && x >= startX && x < startX + logoW && y >= startY && y < startY + logoH)

            {

                // Simple Nearest Neighbor scaling for CPU efficiency

                int lx = (int)((x - startX) / scale);

                int ly = (int)((y - startY) / scale);

                

                if (lx < logoSurface.Width && ly < logoSurface.Height)

                {

                    ColorBgra32 fg = logoSurface[lx, ly];

                    // Manual Alpha Blending

                    float alpha = (fg.A / 255f) * opacity;

                    bg.R = (byte)(fg.R * alpha + bg.R * (1 - alpha));

                    bg.G = (byte)(fg.G * alpha + bg.G * (1 - alpha));

                    bg.B = (byte)(fg.B * alpha + bg.B * (1 - alpha));

                }

            }

            outputRegion[x, y] = bg;

        }

    }

}


protected override void OnDispose(bool disposing)

{

    if (disposing) logoSurface?.Dispose();

    base.OnDispose(disposing);

}


Save the script in your Documents folder - I've created a separate folder named WatermarkLogo and saved the code files as WatermarkLogoCPU.cs

Save the script in your Documents folder - I've created a separate folder named WatermarkLogo and saved the code files as WatermarkLogoCPU.cs

Step 6 - Build the custom Watermark effect.

Click the "Build DLL" menu button in the top-left corner of the CodeLab Editor window.

Click the "Build DLL" menu button in the top-left corner of the CodeLab Editor window.

In the "Building" dialog, make the changes shown in the image below and press the "Build" button.

In the "Building" dialog, make the changes shown in the image below and press the "Build" button.

Click the "OK" button in the "Build Finished" dialog box. An archive containing the DLL has been created on your desktop.

Click the "OK" button in the "Build Finished" dialog box. An archive containing the DLL has been created on your desktop.

Step 7 - Install the custom Watermark effect.

Find the WatermarkLogoCPU.zip archive on the Desktop, created by CodeLab plugin.

Find the WatermarkLogoCPU.zip archive on the Desktop, created by CodeLab plugin.

Unzip archive on desktop.

Unzip paint net plugin archive on desktop

Open the unzipped folder and run the install bat script by double-clicking on it.

Open the unzipped folder and run the install bat script by double-clicking on it.

Press Enter once patching is complete.

Press Enter once patching is complete.

Step 8 - Test custom Watermark effect.

After a successful custom plugin installation, restart Paint Net and find our watermark effect in the top menu.

After a successful custom plugin installation, restart Paint Net and find our watermark effect in the top menu.


Set the margin, scale, and opacity in the Watermark Logo dialog window, and then press the OK button.

Set the margin, scale, and opacity in the Watermark Logo dialog window, and then press the OK button.


Step 9 - How to delete a custom Watermark effect.

To delete our custom Watermark Logo effect we need do following steps : 

- first close Paint Net application  

- than find Paint Net folder in Program Files, 

- than find folder Effects 

- find  WatermarkLogoCPU.dll and delete it

After that restart Paint Net application.

delete a custom Watermark effect


Well Done

So we did something useful today instead of  baking brane with mindless scrolling of social networks :) Good job 


No comments:

Post a Comment