Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Paint.net filetype plugins

Tags:

c#

paint.net

Can't find any info on how to write a filetype plugin for Paint.net. I have found only visual studio template at http://forums.getpaint.net/index.php?/topic/7984-filetype-plugin-template-v20/page__p__121651&#entry121651

and small description at codeproject, but I don't understand all of the parameters of OnSave event handler, what is PaintDotNet.Surface and how to work with data stored there.

like image 972
kalan Avatar asked Jan 22 '23 20:01

kalan


1 Answers

I've written a plug-in like this once. Forget about the template for a minute, you can do this from scratch.

Start by adding references to PaintDotnet.Base, PaintDotNet.Core and PaintDotNet.Data.

Next you'll need a class which inherits from the FileType class:

For example:

public class SampleFileType : FileType
{
    public SampleFileType() : base ("Sample file type", FileTypeFlags.SupportsSaving |
                                FileTypeFlags.SupportsLoading,
                                new string[] { ".sample" })
    {

    }

    protected override void OnSave(Document input, System.IO.Stream output, SaveConfigToken token, Surface scratchSurface, ProgressEventHandler callback)
    {
        //Here you get the image from Paint.NET and you'll have to convert it
        //and write the resulting bytes to the output stream (this will save it to the specified file)

        using (RenderArgs ra = new RenderArgs(new Surface(input.Size)))
        {
            //You must call this to prepare the bitmap
                    input.Render(ra);

            //Now you can access the bitmap and perform some logic on it
            //In this case I'm converting the bitmap to something else (byte[])
            var sampleData = ConvertBitmapToSampleFormat(ra.Bitmap);

            output.Write(sampleData, 0, sampleData.Length);                
        }
    }

    protected override Document OnLoad(System.IO.Stream input)
    {
        //Input is the binary data from the file
        //What you need to do here is convert that date to a valid instance of System.Drawing.Bitmap
        //In the end you need to return it by Document.FromImage(bitmap)

        //The ConvertFromFileToBitmap, just like the ConvertBitmapSampleFormat,
        //is simply a function which takes the binary data and converts it to a valid instance of System.Drawing.Bitmap
        //You will have to define a function like this which does whatever you want to the data

        using(var bitmap = ConvertFromFileToBitmap(input))
        {
            return Document.FromImage(bitmap);
        }
    }
}

So, you inherit from FileType. In the constructor you specify what operations are supported (Loading/Saving) and which file extensions should be registered. Then you provide logic for both the Save and Load operations.

Basically that is all you need.

Finally you'll have to tell Pain.Net which FileType classes you want to load, in this case a single instance, but you could have more then one in a single library.

public class SampleFileTypeFactory : IFileTypeFactory
{
    public FileType[] GetFileTypeInstances()
    {
        return new FileType[] { new SampleFileType() };
    }

I hope this helps, let me know if you have questions. }

like image 50
TimothyP Avatar answered Feb 06 '23 11:02

TimothyP