Jump to content

How it's done?


ReMake

Recommended Posts

I'm trying to write an image loading procedure in CodeLab. This works when loading an image for the first time. When I load a new image, the canvas remains unchanged and contains the image that was loaded for the first time.

This is the source code:

Spoiler

// Name:
// Submenu:
// Author:
// Title:
// Version:
// Desc:
// Keywords:
// URL:
// Help:
#region UICode
FilenameControl image = @""; // Image|png|jpg|gif|bmp
#endregion

private Surface _img = null;
private string imgPath;

protected Surface img
{
    get
    {
        if (_img != null)
        {
            return _img;
        }
        GetImageFromFile();
        return _img;
    }
}

private void GetImageFromFile()
{
    imgPath = image;
    Bitmap bitmap = null;
    try
    {
        bitmap = (Bitmap)Image.FromFile(imgPath, false);
    }
    catch (Exception)
    {
    }
    if (bitmap != null)
    {
        _img = Surface.CopyFromBitmap(bitmap);
        return;
    }
    _img = null;
}

void Render(Surface dst, Surface src, Rectangle rect)
{
    // Delete any of these lines you don't need
    Rectangle selection = EnvironmentParameters.SelectionBounds;
    
    ColorBgra CurrentPixel;
    for (int y = rect.Top; y < rect.Bottom; y++)
    {
        if (IsCancelRequested) return;
        for (int x = rect.Left; x < rect.Right; x++)
        {
            CurrentPixel = src[x,y];

            if (img != null) CurrentPixel = img.GetBilinearSampleWrapped(x,y);

            dst[x,y] = CurrentPixel;
        }
    }
}

 

 

How can I fix this?

Link to comment
Share on other sites

This code has two issues:

 

- After _img is successfully set, it never gets invalidated (set to null), so GetImageFromFile() will never run more than once.

- Conversely, if an image file is not successfully loaded into _img, GetImageFromFile() will run on every single pixel, and fail over and over again. A serious performance issue.

 

Did you find this code somewhere on the forum? If so, it needs to dealt with. It's bad code, and shouldn't be used.

 

I would just recommend doing this stuff in PreRender().

(September 25th, 2023)  Sorry about any broken images in my posts. I am aware of the issue.

bp-sig.png
My Gallery  |  My Plugin Pack

Layman's Guide to CodeLab

Link to comment
Share on other sites

Yup, that example code also has the same two issues I pointed out.

@Illnab1024 wrote that code a very long time ago; before IClipboardService and OnSetRenderInfo() existed in Paint.NET.

(September 25th, 2023)  Sorry about any broken images in my posts. I am aware of the issue.

bp-sig.png
My Gallery  |  My Plugin Pack

Layman's Guide to CodeLab

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.

Guest
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...