MJW Posted December 13, 2018 Share Posted December 13, 2018 This is a very simple plugin originally intended to erase pixels in the canvas that don't match the clipboard, but extended so that it can optionally keep them instead. The clipboard image must be the same size as the canvas; otherwise, the effect does nothing. For no particularly good reason, other than I couldn't think of anything better, it's in the Color menu. The plugin: MismatchEraser.zip It's a CodLab plugin. Here is the source code: Spoiler // Author: MJW // Name: Mismatch Eraser // Title: Mismatch Eraser // Submenu: color // Desc: Keep or erase pixels that match the clipboard. // Keywords: erase mismatch clipboard #region UICode RadioButtonControl Amount1 = 0; // [1] Action|Keep Pixels That Match Clipboard|Erase Pixels That Match Clipboard #endregion // Here is the main render loop function void Render(Surface dst, Surface src, Rectangle rect) { bool haveCbImage = (img != null) && (src.Width == img.Width) && (src.Height == img.Height); for (int y = rect.Top; y < rect.Bottom; y++) { if (IsCancelRequested) return; for (int x = rect.Left; x < rect.Right; x++) { ColorBgra CbPixel; ColorBgra CurrentPixel = src[x, y]; if (IsCancelRequested) return; // If clipboard has an image, get it. Check every time. if (haveCbImage) haveCbImage = (img != null); CbPixel = haveCbImage ? img[x, y] : CurrentPixel; if (Amount1 == 0) dst[x, y] = (CurrentPixel == CbPixel) ? CurrentPixel : ColorBgra.Transparent; else dst[x, y] = (CurrentPixel != CbPixel) ? CurrentPixel : ColorBgra.Transparent; } } } // Setup for getting an image from the clipboard protected Surface img { get { if (_img != null) return _img; else { System.Threading.Thread t = new System.Threading.Thread(new System.Threading.ThreadStart(GetImageFromClipboard)); t.SetApartmentState(System.Threading.ApartmentState.STA); t.Start(); t.Join(); return _img; } } } private Surface _img = null; private void GetImageFromClipboard() { Bitmap aimg = null; System.Windows.Forms.IDataObject clippy; try { clippy = System.Windows.Forms.Clipboard.GetDataObject(); if (clippy != null) { if (System.Windows.Forms.Clipboard.ContainsData("PNG")) { // Handle bitmaps with transparency Object png_object = System.Windows.Forms.Clipboard.GetData("PNG"); if (png_object is System.IO.MemoryStream) { System.IO.MemoryStream png_stream = png_object as System.IO.MemoryStream; aimg = (Bitmap)Image.FromStream(png_stream); } } else if (clippy.GetDataPresent(System.Windows.Forms.DataFormats.Bitmap)) { // If that didn't work, try bitmaps without transparency aimg = (Bitmap)clippy.GetData(typeof(System.Drawing.Bitmap)); } } } catch (Exception) { } if (aimg != null) { _img = Surface.CopyFromBitmap(aimg); } else { _img = null; } } 3 3 Quote Link to comment Share on other sites More sharing options...
Pixey Posted December 13, 2018 Share Posted December 13, 2018 Great Gonna give this a try 🧐 Quote How I made Jennifer & Halle in Paint.net My Gallery | My Deviant Art "Rescuing one animal may not change the world, but for that animal their world is changed forever!" anon. Link to comment Share on other sites More sharing options...
Seerose Posted December 14, 2018 Share Posted December 14, 2018 @MJW! Thank you very much for the great plugin. Quote Live as if you were to die tomorrow. Learn as if you were to live forever. Gandhi 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.