ReMake Posted May 18, 2020 Share Posted May 18, 2020 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? Quote Link to comment Share on other sites More sharing options...
toe_head2001 Posted May 18, 2020 Share Posted May 18, 2020 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(). Quote (September 25th, 2023) Sorry about any broken images in my posts. I am aware of the issue. My Gallery | My Plugin Pack Layman's Guide to CodeLab Link to comment Share on other sites More sharing options...
ReMake Posted May 18, 2020 Author Share Posted May 18, 2020 47 minutes ago, toe_head2001 said: Did you find this code somewhere on the forum? No. I tried to create it myself using loading an image from the Clipboard, as described in Part 4 of BoltBait's CodeLab Tutorial. Quote Link to comment Share on other sites More sharing options...
toe_head2001 Posted May 18, 2020 Share Posted May 18, 2020 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. Quote (September 25th, 2023) Sorry about any broken images in my posts. I am aware of the issue. My Gallery | My Plugin Pack Layman's Guide to CodeLab Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.