Jump to content

Saving selection coordinates


Recommended Posts

It wouldn't be too hard of an effect to write.

 

What format would you like to save the coordinates in?  (IOW, what would you like the text file to look like?)

 

EDIT: It has been a few minutes and you haven't responded to my question.  So, I wrote a generic CodeLab script that does what you want:

 

// Name: Output Selection Coordinates
// Author: BoltBait
// Version: 1.0
#region UICode
#endregion

string OutputFilename = @"C:\Users\YourName\Desktop\output.txt";

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 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 (x == CenterX && y == CenterY)
            {
                if (!File.Exists(OutputFilename))
                {
                    using (StreamWriter sw = File.CreateText(OutputFilename))
                    {
                        sw.WriteLine(selection.Left.ToString() + "," + selection.Top.ToString() + " " + selection.Right.ToString() + "," + selection.Bottom.ToString());
                    }
                }
                else
                {
                    using (StreamWriter sw = File.AppendText(OutputFilename))
                    {
                        sw.WriteLine(selection.Left.ToString() + "," + selection.Top.ToString() + " " + selection.Right.ToString() + "," + selection.Bottom.ToString());
                    }
                }
            }
            dst[x,y] = CurrentPixel;
        }
    }
}

 

  • Upvote 2
Link to comment
Share on other sites

 

Codelab is a paint.net plugin which creates other plugins. Find it by clicking the link in BoltBaits signature. You will need to compile the script he gave you to make your own plugin. If that seems too much trouble, I'd just write them down from the info in the  status bar :smile:

Link to comment
Share on other sites

OK, here is a more complicated script that should do what you want.

 

// Name: Output Selection Coordinates
// Author: BoltBait
// Version: 1.1
#region UICode
byte Amount1 = 0; // [255] Select Current Image File
#endregion

string OutputFilename = "";
int PreviousBrowseButton = -1;
string imgPath = null;

void GetFileName()
{
    OpenFileDialog ofd = new OpenFileDialog();
    ofd.Title = "Select Image File";
    ofd.Filter = "Image Files(*.PNG;*.BMP;*.JPG;*.GIF)|*.PNG;*.BMP;*.JPG;*.GIF|All files (*.*)|*.*";
    ofd.DefaultExt = ".png";
    ofd.Multiselect = false;

    if (ofd.ShowDialog() == DialogResult.OK)
    {
        imgPath = ofd.FileName;
        OutputFilename = Path.ChangeExtension(imgPath, ".txt");
    }
    else
    {
        OutputFilename = "";
    }
}

void Render(Surface dst, Surface src, Rectangle rect)
{
    if (PreviousBrowseButton == -1)
    {
        PreviousBrowseButton = Amount1;
    }
    if (PreviousBrowseButton != Amount1)
    {
        PreviousBrowseButton = Amount1;
        Form IndirectUIForm = Form.ActiveForm;
        if (IndirectUIForm != null)
        {
            IndirectUIForm.Invoke(new Action(delegate ()
            {
                GetFileName();
            }));
        }
    }
    
    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 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 (x == CenterX && y == CenterY)
            {
                if (OutputFilename != "")
                {
                    if (!File.Exists(OutputFilename))
                    {
                        using (StreamWriter sw = File.CreateText(OutputFilename))
                        {
                            sw.WriteLine(selection.Left.ToString() + "," + selection.Top.ToString() + " " + selection.Right.ToString() + "," + selection.Bottom.ToString());
                        }
                    }
                    else
                    {
                        using (StreamWriter sw = File.AppendText(OutputFilename))
                        {
                            sw.WriteLine(selection.Left.ToString() + "," + selection.Top.ToString() + " " + selection.Right.ToString() + "," + selection.Bottom.ToString());
                        }
                    }
                }
            }
            dst[x,y] = CurrentPixel;
        }
    }
}

 

How to use

 

Download the following zip file (I used CodeLab to compile it for you): WriteSelectionCoordinatesToFile.zip

Unzip it to your desktop.  Run the enclosed Install_.bat file to install the effect into your paint.net

 

Open an image in paint.net and make a selection.

Run the effect.

Click the button to select your image file and click OK. (This is necessary because effects do not know the name of the open file.)

Click OK on the effect dialog box to finalize your effect and write the coordinates to the file.

 

Hope this helps!

 

  • Upvote 1
Link to comment
Share on other sites

I don't really feel like writing a tutorial on this one.  You better just bookmark it.

 

The script actually has issues.  For example, I can't get it to append text to the existing file.  My intent was to make a selection, run the effect. Make another selection, run the effect again with the result being 2 lines in the output file.  It doesn't work... and I'm too lazy to debug it.

Link to comment
Share on other sites

But it's fine, it does append text to the existing file.

 

The only inconvenience is that I need to select the file at least two times: when I open the image and when I save the coordinates.

 

Any chance for something like this?

- I run the script

- it requests an image file

- I select the image file

- it opens it

- it waits

- I make the selection

- it saves it to the text file

 

Link to comment
Share on other sites

OK, don't tell Rick that I did this, but here is a plugin that is NOT well behaved that does what you want.

 

I'll leave it to you to build using CodeLab.

 

// Name: Output Selection Coordinates
// Author: BoltBait
// Version: 2.0
#region UICode
#endregion

string LatestFileName()
{
    string filename = Form.ActiveForm.Owner.Text;
    filename = filename.Substring(0, filename.IndexOf(" - "));
    if (filename != "Untitled")
    {
        for (int i=7; i>=0; i--)
        {
            string regpath = @"HKEY_CURRENT_USER\Software\Paint.NET";
            string regkey = "File/MostRecent/Path" + i.ToString();
            string regval = (string)Registry.GetValue(regpath,regkey,"Untitled");
            if (filename == Path.GetFileName(regval))
            {
                return regval;
            }
        }
    }
    return filename;
}

void GetFileName()
{
    OpenFileDialog ofd = new OpenFileDialog();
    ofd.Title = "Select Image File";
    ofd.Filter = "Image Files(*.PNG;*.BMP;*.JPG;*.GIF)|*.PNG;*.BMP;*.JPG;*.GIF|All files (*.*)|*.*";
    ofd.DefaultExt = ".png";
    ofd.Multiselect = false;

    if (ofd.ShowDialog() == DialogResult.OK)
    {
        imgPath = ofd.FileName;
        OutputFilename = Path.ChangeExtension(imgPath, ".txt");
    }
    else
    {
        OutputFilename = "";
    }
}

string OutputFilename = "";
string imgPath = null;

void Render(Surface dst, Surface src, Rectangle rect)
{
    if (OutputFilename == "Cancelled") return;
    if (OutputFilename == "")
    {
        OutputFilename = LatestFileName();
        if (OutputFilename == "Untitled" || OutputFilename == "")
        {
            Form IndirectUIForm = Form.ActiveForm;
            if (IndirectUIForm != null)
            {
                IndirectUIForm.Invoke(new Action(delegate ()
                {
                    GetFileName();
                }));
            }
        }
        else
        {
            OutputFilename = Path.ChangeExtension(OutputFilename, ".txt");
        }
    }
    if (OutputFilename == "")
    {
        OutputFilename = "Cancelled";
        return;
    }
    if (!Path.IsPathRooted(OutputFilename))
    {
        string dir = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
        OutputFilename = Path.Combine(dir, Path.GetFileName(OutputFilename));
    }

    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;
    string OutputData = selection.Left.ToString() + "," + selection.Top.ToString() + " " + selection.Right.ToString() + "," + selection.Bottom.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];
            if (x == CenterX && y == CenterY)
            {
                if (OutputFilename != "")
                {
                    if (!File.Exists(OutputFilename))
                    {
                        using (StreamWriter sw = File.CreateText(OutputFilename))
                        {
                            sw.WriteLine(OutputData);
                        }
                    }
                    else
                    {
                        using (StreamWriter sw = File.AppendText(OutputFilename))
                        {
                            sw.WriteLine(OutputData);
                        }
                    }
                    Form IndirectUIForm = Form.ActiveForm;
                    if (IndirectUIForm != null)
                    {
                        IndirectUIForm.Invoke(new Action(delegate ()
                        {
                            MessageBox.Show("Data saved to file:\r\n" + OutputFilename + "\r\n\r\nData: " + OutputData);
                        }));
                    }
                }
            }
            dst[x,y] = CurrentPixel;
        }
    }
}

 

And, you fellow Effect authors, avert your eyes!  Do not follow down the path of the unrighteous!

 

I'm serious! Rick will probably ban me for posting such unclean code.

 

Press your browser's back button now before you become corrupted!

 

MJW, don't you dare use this!

 

  • Upvote 2
Link to comment
Share on other sites

Yeah Eli. Let that be a lesson to you! :lol:

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