Jump to content

codelab code excel files


roro69
 Share

Recommended Posts

The best way might be to output your data in CSV.

Something like this:

 

// Name: CSV Output Example
// Author: BoltBait
#region UICode
TextboxControl Amount1 = ""; // [0,255] Output Pathname
#endregion

void Render(Surface dst, Surface src, Rectangle rect)
{
    Rectangle selection = EnvironmentParameters.GetSelection(src.Bounds).GetBoundsInt();
    int CenterX = ((selection.Right - selection.Left) / 2)+selection.Left;
    int CenterY = ((selection.Bottom - selection.Top) / 2)+selection.Top;
    ColorBgra PrimaryColor = (ColorBgra)EnvironmentParameters.PrimaryColor;
    ColorBgra SecondaryColor = (ColorBgra)EnvironmentParameters.SecondaryColor;
    int BrushWidth = (int)EnvironmentParameters.BrushWidth;
    
    // create a list of strings
    List<string> lines = new List<string>();
    // populate that list with everything you wish to output using "," as a separator
    lines.Add("CenterX, " + CenterX.ToString());
    lines.Add("CenterY, " + CenterY.ToString());
    lines.Add("PrimaryColor, " + PrimaryColor.ToHexString());
    lines.Add("SecondaryColor, " + SecondaryColor.ToHexString());
    lines.Add("BrushWidth, " + BrushWidth.ToString());
    
    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];

            // See if we are working on the center pixel
            if (x == CenterX && y == CenterY)
            {
                // Do we have a valid filename?
                if (Amount1.EndsWith(".csv"))
                {
                    // Output everything to the file
                    File.WriteAllLines(Amount1, lines);
                }
            }
            
            dst[x,y] = CurrentPixel;
        }
    }
}
You can then open your CSV file in Excel.

Click to play:
j.pngs.pngd.pnga.pngp.png
Download: BoltBait's Plugin Pack | CodeLab | and how about a Computer Dominos Game

Link to comment
Share on other sites

  • 3 weeks later...
Hello

Sorry for the delay in replying; it was super helpful to have me answer ainsi.Je went t through visual studio to create my plugin, I debug (like here : http://forums.getpaint.net/index.php?/topic/4209-how-to-debug-your-plugin/)and built my dll; but it does not load in net paint .:

Should I change the framework? (I work with the framework 4.5)

Thank you for your answers and time spent.

Sorry for my English ; I am French and I have a little trouble with English

Link to comment
Share on other sites

It does not load? Does it give an error in Settings > Plugin Errors http://www.getpaint.net/doc/latest/SettingsDialog.html or does it simply not show up?

If it doesn't show up, read this http://forums.getpaint.net/index.php?/topic/4209-how-to-debug-your-plugin/#entry343340

Link to comment
Share on other sites

If you are referencing paint.net v4.x files, you need to compile with .NET 4.5.x (4.6.x will also work)

Visual Studio will output Warnings if you try to compile with .NET 4.0 or lower.

Link to comment
Share on other sites

Good evening
Thank you for your help my code works fine; the problem was the icon file.
If anyone here interested here is the code to write to a file excel to count pixel (This is not an effect plugin) .....
using System;
using System.Text;
using System.Windows;
using System.Reflection;
using System.Windows.Forms;
using PaintDotNet;
using PaintDotNet.Effects;
using PaintDotNet.IndirectUI;
using PaintDotNet.PropertySystem;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Text;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using IntSliderControl = System.Int32;
using TextboxControl = System.String;
using  Microsoft.Office.Interop;

namespace PatronLivre
{
    public class PluginSupportInfo : IPluginSupportInfo
    {
        public string Author
        {
            get
            {
                return ((AssemblyCopyrightAttribute)base.GetType().Assembly.GetCustomAttributes(typeof(AssemblyCopyrightAttribute), false)[0]).Copyright;
            }
        }
        public string Copyright
        {
            get
            {
                return ((AssemblyDescriptionAttribute)base.GetType().Assembly.GetCustomAttributes(typeof(AssemblyDescriptionAttribute), false)[0]).Description;
            }
        }

        public string DisplayName
        {
            get
            {
                return ((AssemblyProductAttribute)base.GetType().Assembly.GetCustomAttributes(typeof(AssemblyProductAttribute), false)[0]).Product;
            }
        }

        public Version Version
        {
            get
            {
                return base.GetType().Assembly.GetName().Version;
            }
        }

        public Uri WebsiteUri
        {
            get
            {
                return new Uri("http://www.getpaint.net/redirect/plugins.html");
            }
        }
    }


    [PluginSupportInfo(typeof(PluginSupportInfo), DisplayName = "CalcPat+")]
    [EffectCategory(EffectCategory.Effect)]
    public class MonEffetEffectPlugin : PropertyBasedEffect
    {
        public static string StaticName
        {
            get
            {
                return "CalculPatron";
            }
        }

        public static Bitmap StaticIcon { get { return PatronLivreEffect.Properties.Resources.EffectPluginIcon; } }
        public static string SubmenuName
        {
            get
            {
                return SubmenuNames.Render;  // Programmer's chosen default
            }
        }
        public MonEffetEffectPlugin()
            : base(StaticName, StaticIcon, SubmenuName, EffectFlags.Configurable)
        {
            instanceSeed = unchecked((int)DateTime.Now.Ticks);
        }
        public enum PropertyNames
        {
            Amount1,
            Amount2,
            Amount3
        }

        [ThreadStatic]
        private static Random RandomNumber;

        private int randomSeed;
        private int instanceSeed;


        protected override PropertyCollection OnCreatePropertyCollection()
        {
            List<Property> props = new List<Property>();

            props.Add(new Int32Property(PropertyNames.Amount1, 0, 0, 1000));
            props.Add(new StringProperty(PropertyNames.Amount2, "", 255));
            props.Add(new Int32Property(PropertyNames.Amount3, 0, 0, 255));

            return new PropertyCollection(props);
        }

        protected override ControlInfo OnCreateConfigUI(PropertyCollection props)
        {
            ControlInfo configUI = CreateDefaultConfigUI(props);

            configUI.SetPropertyControlValue(PropertyNames.Amount1, ControlInfoPropertyNames.DisplayName, "Nombre Page Livre");
            configUI.SetPropertyControlValue(PropertyNames.Amount2, ControlInfoPropertyNames.DisplayName, "Nom du Patron");
            configUI.SetPropertyControlValue(PropertyNames.Amount3, ControlInfoPropertyNames.DisplayName, string.Empty);
            configUI.SetPropertyControlType(PropertyNames.Amount3, PropertyControlType.IncrementButton);
            configUI.SetPropertyControlValue(PropertyNames.Amount3, ControlInfoPropertyNames.ButtonText, "Sauve Patron");

            return configUI;
        }

        protected override void OnSetRenderInfo(PropertyBasedEffectConfigToken newToken, RenderArgs dstArgs, RenderArgs srcArgs)
        {
            Amount1 = newToken.GetProperty<Int32Property>(PropertyNames.Amount1).Value;
            Amount2 = newToken.GetProperty<StringProperty>(PropertyNames.Amount2).Value;
            Amount3 = (byte)newToken.GetProperty<Int32Property>(PropertyNames.Amount3).Value;
            randomSeed = Amount3;

            base.OnSetRenderInfo(newToken, dstArgs, srcArgs);
        }

        protected override void OnCustomizeConfigUIWindowProperties(PropertyCollection props)
        {
            // Change the effect's window title
            props[ControlInfoPropertyNames.WindowTitle].Value = "CalculPatron";
            base.OnCustomizeConfigUIWindowProperties(props);
        }

        protected override unsafe void OnRender(Rectangle[] rois, int startIndex, int length)
        {
            if (length == 0) return;
            RandomNumber = GetRandomNumberGenerator(rois, startIndex);
            for (int i = startIndex; i < startIndex + length; ++i)
            {
                Render(DstArgs.Surface, SrcArgs.Surface, rois[i]);
            }
        }

        private Random GetRandomNumberGenerator(Rectangle[] rois, int startIndex)
        {
            Rectangle roi = rois[startIndex];
            return new Random(instanceSeed ^ (randomSeed << 16) ^ (roi.X << 8) ^ roi.Y);
        }

        #region User Entered Code
        // Name:
        // Submenu:
        // Author:
        // Title:
        // Version:
        // Desc:
        // Keywords:
        // URL:
        // Help:
        #region UICode
        IntSliderControl Amount1 = 0; // [0,1000] Nombre Page Livre
        TextboxControl Amount2 = ""; // [0,255] Nom du Patron
        byte Amount3 = 0; // [255] Sauve Patron



        #endregion
        byte LastButton = 0;
        String cboard;
        public struct Folds
        {
            public double a, b, c, d;

            public Folds(double p1, double p2, double p3, double p4)
            {
                a = p1;
                b = p2;
                c = p3;
                d = p4;
            }
        }

        public void SaveImages()
        {
           
            System.Windows.Forms.SaveFileDialog SaveFileDialog1 = new System.Windows.Forms.SaveFileDialog();
            string fp = Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
            SaveFileDialog1.InitialDirectory = fp;
            SaveFileDialog1.Filter = "Text file (*.txt)|*.txt";
            SaveFileDialog1.FilterIndex = 1;
            SaveFileDialog1.RestoreDirectory = false;

            if (SaveFileDialog1.ShowDialog() == DialogResult.OK)
            {

                cboard = SaveFileDialog1.FileName;

            }
            else
            {
                cboard = String.Empty;
            }

        }



        // Here is the main render loop function
        void Render(Surface dst, Surface src, Rectangle rect)
        {
            if (!IsCancelRequested)
            {

                Rectangle sel = EnvironmentParameters.GetSelection(src.Bounds).GetBoundsInt();
                dst.CopySurface(src, sel.Location, sel);
                //========================= 
                SignalCancelRequest();
                if (LastButton != Amount3)
                {
                    LastButton = Amount3;
                    System.Threading.Thread t = new System.Threading.Thread(new System.Threading.ThreadStart(SaveImages));
                    t.SetApartmentState(System.Threading.ApartmentState.STA);
                    t.Start();
                    t.Join();
                    Application.DoEvents();
                    if (cboard != String.Empty)
                    {
                        //try {
                        Folds[] folds = new Folds[0];

                        System.IO.StreamWriter pc = new System.IO.StreamWriter(cboard, false);
                        pc.WriteLine("Nom du patron:" + Amount2);
                        pc.WriteLine("----------------------------------------------");
                        pc.Close();
                        GC.Collect();
                        Microsoft.Office.Interop.Excel.Application oXL;
                        Microsoft.Office.Interop.Excel._Workbook oWB;
                        Microsoft.Office.Interop.Excel._Worksheet oSheet;
                        
                        object misvalue = System.Reflection.Missing.Value;
                        //Demarre excel
                        oXL = new Microsoft.Office.Interop.Excel.Application();
                        oXL.Visible = false;
                        //Nouveau classeur
                        oWB = (Microsoft.Office.Interop.Excel._Workbook)(oXL.Workbooks.Add(Missing.Value));
                        oSheet = (Microsoft.Office.Interop.Excel._Worksheet)oWB.ActiveSheet;
                        //Ajouter les entete de cellule

                        oSheet.Cells[1, 1] = "Nom Patron";
                        oSheet.Cells[1, 2] = Amount2;
                        oSheet.Cells[2, 1] = "Page";
                        oSheet.Cells[2, 2] = "Haut";
                        oSheet.Cells[2, 3] = "Bas";
                        


                        
                        for (int i = sel.Left; i < sel.Right; i++)
                        {
                            double toppix = 0;
                            double pixlength = 0;

                            for (int y = sel.Top; y < sel.Bottom; y++)
                            {
                                ColorBgra CP = src.GetBilinearSample(i, y);
                                int total = CP.R + CP.G + CP.B;
                                if (toppix == 0)
                                {
                                    if (total < 765)
                                    {
                                        toppix = y;
                                        pixlength = 1;
                                    }
                                }
                                else
                                {
                                    if (total < 765)
                                    {
                                        pixlength++;
                                    }
                                    else
                                    {


                                        Array.Resize(ref folds, folds.Length + 1);
                                        folds[folds.Length - 1] = new Folds(i, (toppix - 1) / 10, (pixlength + toppix) / 10, 0);


                                        toppix = 0;
                                        pixlength = 0;


                                    }
                                }
                            }
                        }
                        int x = 0;

                        for (int i = 1; i <= folds.Length - 1; i++)
                        {
                            if (folds[i].a == folds[i - 1].a)
                            {
                                folds[x].d = folds[x].d + 1;
                            }
                            else
                            {
                                x = i;
                                folds[x].d = folds[x].d + 1;


                            }
                        }
                        int p = (Amount1 - (folds.Length * 2)) / 2;
                        int ligne = 2;
                        for (int i = 1; i <= folds.Length - 1; i++)
                        {
                            if (folds[i].d == 0)
                            {
                                folds[i].d = folds[i - 1].d;

                            }

                            if ((folds[i].a + folds[i].d + i) % folds[i].d == 0)
                            {
                                p = p + 2;
                                ligne = ligne + 1;

                                System.IO.StreamWriter pc2 = new System.IO.StreamWriter(cboard, true);
                                pc2.WriteLine("Pages : " + p + "| " + "Haut: " + folds[i].b + "  cm" + " | " + "Bas: " + folds[i].c + " cm");
                                pc2.WriteLine("----------------------------------------------");
                                pc2.Close();
                                oSheet.Cells[ligne, 1] = p;
                                oSheet.Cells[ligne, 2] = folds[i].b;
                                oSheet.Cells[ligne, 3] = folds[i].c;
                            }

                        }

                          oSheet.UsedRange.HorizontalAlignment =
                           Microsoft.Office.Interop.Excel.XlVAlign.xlVAlignCenter;
                        oXL.UserControl = false;
                        oWB.SaveAs(Amount2, Microsoft.Office.Interop.Excel.XlFileFormat.xlWorkbookDefault, Type.Missing, Type.Missing,
                    false, false, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange,
                    Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);

                        oWB.Close();
                        oXL.Quit();
                        System.Runtime.InteropServices.Marshal.ReleaseComObject(oXL);
                        System.Runtime.InteropServices.Marshal.ReleaseComObject(oWB);
                        System.Runtime.InteropServices.Marshal.ReleaseComObject(oSheet);
                        oSheet = null;
                        oWB = null;
                        oXL = null;
                        GC.Collect();
                    }

                    //}catch(Exception e){}
                }//cboard

            }//last button

        }


        #endregion
    }
}

Thank you for your help and time spent

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.

 Share

×
×
  • Create New...