Jump to content
Paint.NET 5.1 is now available! ×

Recommended Posts

Posted (edited)

Sorry for my bad English.

 

I have a list of EffectConfigToken objects from many effects (Brightness / Contrast, Curves,...), how can I serialize it to write to/read from a file on disk?

Edited by AuSang
  • AuSang changed the title to How to Write/Read EffectConfigToken list to/from files?
Posted

This is how I do it inside a plugin....

            using (SaveFileDialog svfd = new SaveFileDialog())
            {
                svfd.FileName = filetitle;
                // get last used path
                svfd.InitialDirectory = GetlastFolder();
                // look for *.pln files
                svfd.Filter = "Planetoid Files (.pln)|*.pln|All Files (*.*)|*.*";
                svfd.FilterIndex = 1;
                // Show the filename extension
                svfd.AddExtension = true;

                if (svfd.ShowDialog() == DialogResult.OK)
                {
					// create a new instance of the token.
                    FinishTokenUpdate();
                    EffectPluginConfigToken PlanetToken = (EffectPluginConfigToken)theEffectToken;

                    // set up XML serialized of type EffectPluginConfigToken
                    XmlSerializer xSer = new XmlSerializer(typeof(EffectPluginConfigToken));

                    using (FileStream stream = File.Open(svfd.FileName, FileMode.Create))
                    {
                        // serialize it.
                        xSer.Serialize(stream, PlanetToken);
                    }
                }
            }

What are you trying to do with the data?

Posted (edited)
7 hours ago, Ego Eram Reputo said:

This is how I do it inside a plugin....


            using (SaveFileDialog svfd = new SaveFileDialog())
            {
                svfd.FileName = filetitle;
                // get last used path
                svfd.InitialDirectory = GetlastFolder();
                // look for *.pln files
                svfd.Filter = "Planetoid Files (.pln)|*.pln|All Files (*.*)|*.*";
                svfd.FilterIndex = 1;
                // Show the filename extension
                svfd.AddExtension = true;

                if (svfd.ShowDialog() == DialogResult.OK)
                {
					// create a new instance of the token.
                    FinishTokenUpdate();
                    EffectPluginConfigToken PlanetToken = (EffectPluginConfigToken)theEffectToken;

                    // set up XML serialized of type EffectPluginConfigToken
                    XmlSerializer xSer = new XmlSerializer(typeof(EffectPluginConfigToken));

                    using (FileStream stream = File.Open(svfd.FileName, FileMode.Create))
                    {
                        // serialize it.
                        xSer.Serialize(stream, PlanetToken);
                    }
                }
            }

What are you trying to do with the data?

Thank you for giving me an advice.

 

We're working on final project at school, on which we have to write a plugin to manage presets/filters like Lightroom or other image processing applications, our MyEffectConfigToken includes a List<Pair<Effect, EffectConfigToken>> and I want to write it down on disk. If you have any better idea, please let us know.

 

Thanks again.

 

*Edit: I've found that I can serialize the Property of PropertyBasedEffect effects, but I don't know how to deal with "non-property-based" ones.

 

Edited by AuSang
Posted

You need to flag them as [serializable]  and (maybe) write your own serializer. It is not as difficult as it might sound. I did this with my Planetoid plugin because the UI colors were not being serialized correctly: https://forums.getpaint.net/topic/28713-planetoid-make-your-own-planets/?do=findComment&comment=540203

 

I can post some code later today.

 

 

 

Posted (edited)
5 hours ago, Ego Eram Reputo said:

You need to flag them as [serializable]  and (maybe) write your own serializer. It is not as difficult as it might sound. I did this with my Planetoid plugin because the UI colors were not being serialized correctly: https://forums.getpaint.net/topic/28713-planetoid-make-your-own-planets/?do=findComment&comment=540203

 

I can post some code later today.

 

 

 

Hi, I've done file handling for property-based effects, you can visit my work at https://github.com/auduongtansang/PDNPresets/tree/file-operation. I'm going to work with the other effect type and really looking forward to your advice.

Edited by AuSang
Posted

Here's the basic premise to replacing an XML element. In your effect token *.CS flie,

  • We tell the serializer to ignore the element using [XmlIgnore]
  • We tell the serializer what we want to replace the element with using [XmlElement(element)]
        public Color token_OceanColor;
        //
        // added 22/10/2016 Color is not serialized! To get around this we need to tell the serializer to ignore Color
        //
        [XmlIgnore]
        public Color XMLtoken_OceanColor
        {
            get { return token_OceanColor; }
            set { token_OceanColor = value; }
        }
        //
        // Then provide a substutute which IS serializable....
        //
        [XmlElement("XMLtoken_OceanColor")]
        public string XMLtoken_OceanColorHtml
        {
            get { return string.Format("#{0:X2}{1:X2}{2:X2}{3:X2}", token_OceanColor.A, token_OceanColor.R, token_OceanColor.G, token_OceanColor.B); }
            set { XMLtoken_OceanColor = ColorTranslator.FromHtml(value); }
        }

^ in this case I'm replacing a Color with an ARGB string. When serializing, XMLtoken_OceanColor's Color is replaced with the string XMLtoken_OceanColorHtml

 

 

Posted
17 hours ago, AuSang said:

*Edit: I've found that I can serialize the Property of PropertyBasedEffect effects, but I don't know how to deal with "non-property-based" ones.

 

Many of the non-property-based effects should have a serializable effect token.

The effect classes are not serializable, you would have to serialize the value returned by Effect.GetType.

 

18 hours ago, AuSang said:

We're working on final project at school, on which we have to write a plugin to manage presets/filters like Lightroom or other image processing applications, our MyEffectConfigToken includes a List<Pair<Effect, EffectConfigToken>> and I want to write it down on disk. If you have any better idea, please let us know.

 

You may want to look at the source code for @pyrochild's ScriptLab (https://github.com/bsneeze/pdn-scriptlab).

It is a plugin that allows the user to save a list of Effect presets to a file, and reapply them at a later time.

PdnSig.png

Plugin Pack | PSFilterPdn | Content Aware Fill | G'MICPaint Shop Pro Filetype | RAW Filetype | WebP Filetype

The small increase in performance you get coding in C++ over C# is hardly enough to offset the headache of coding in the C++ language. ~BoltBait

 

Posted
On 6/30/2020 at 12:09 PM, Ego Eram Reputo said:

Here's the basic premise to replacing an XML element. In your effect token *.CS flie,

  • We tell the serializer to ignore the element using [XmlIgnore]
  • We tell the serializer what we want to replace the element with using [XmlElement(element)]

        public Color token_OceanColor;
        //
        // added 22/10/2016 Color is not serialized! To get around this we need to tell the serializer to ignore Color
        //
        [XmlIgnore]
        public Color XMLtoken_OceanColor
        {
            get { return token_OceanColor; }
            set { token_OceanColor = value; }
        }
        //
        // Then provide a substutute which IS serializable....
        //
        [XmlElement("XMLtoken_OceanColor")]
        public string XMLtoken_OceanColorHtml
        {
            get { return string.Format("#{0:X2}{1:X2}{2:X2}{3:X2}", token_OceanColor.A, token_OceanColor.R, token_OceanColor.G, token_OceanColor.B); }
            set { XMLtoken_OceanColor = ColorTranslator.FromHtml(value); }
        }

^ in this case I'm replacing a Color with an ARGB string. When serializing, XMLtoken_OceanColor's Color is replaced with the string XMLtoken_OceanColorHtml

 

 

Thank you, I got that. I'm now using BinaryWriter to manually write my properties down.

On 6/30/2020 at 1:28 PM, null54 said:

 

Many of the non-property-based effects should have a serializable effect token.

The effect classes are not serializable, you would have to serialize the value returned by Effect.GetType.

 

 

You may want to look at the source code for @pyrochild's ScriptLab (https://github.com/bsneeze/pdn-scriptlab).

It is a plugin that allows the user to save a list of Effect presets to a file, and reapply them at a later time.

Thanks, but ScriptLab was last update 4 years ago. There are many things, which I need, unsupported by new versions of Paint.NET.

Posted
On 6/29/2020 at 11:28 PM, null54 said:

Many of the non-property-based effects should have a serializable effect token.

The effect classes are not serializable, you would have to serialize the value returned by Effect.GetType.

 

All effect config tokens should be binary serializable -- and marked with the [Serializable] attribute.

 

This is used to implement "Effect -> Repeat", as well as re-populating an effect config dialog with its old values when you launch the same effect again.

 

So ... all effect config tokens should be serializable already. Out of the box. Can't speak for plugins, obviously, but if they work with Effect->Repeat then it should be no problem.

The Paint.NET Blog: https://blog.getpaint.net/

Donations are always appreciated! https://www.getpaint.net/donate.html

forumSig_bmwE60.jpg

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