Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / Drawing / March 2008

Tip: Looking for answers? Try searching our database.

System.Windows.Media.Color

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Christopher Ireland - 10 Mar 2008 18:31 GMT
Hello!

Thanks to microsoft.public.dotnet.languages.csharp, I now know that the
structure System.Windows.Media.Color cannot be serialized.

Before I start on a lengthy process to serialize this structure by hand
(lengthy because of the number of properties I have of this type the code I
work with), I wonder if anybody "in the know" with contacts in Microsoft
could tell me if this is an oversight or if this structure will never be
able to be automatically serialized in future .net versions.

Thank you very much for any answers!

Signature

Thank you,

Christopher Ireland
http://shunyata-kharg.doesntexist.com

"If you are always busy doing something, you cannot enjoy the world."
Lao Tzu

RobinS - 11 Mar 2008 02:36 GMT
You've probably hashed this out in the C# group, but I has to ask: Have you
considered serializing r, g, b, and a, or the ToString of ToArgb and then
using ToArgb and FromArgb to convert back and forth?

I am serializing a System.Drawing.Color value by serializing the string
representation, and then using a property of type Color that uses a
converter to convert to and from the string.

Just wondering.
RobinS.
GoldMail, Inc.
-----------------------------
> Hello!
>
[quoted text clipped - 8 lines]
>
> Thank you very much for any answers!
Christopher Ireland - 11 Mar 2008 08:14 GMT
RobinS,

Many thanks for your reply!

> You've probably hashed this out in the C# group, but I has to ask:
> Have you considered serializing r, g, b, and a, or the ToString of
> ToArgb and then using ToArgb and FromArgb to convert back and forth?

Well, the thread on the C# group is here:
http://groups.google.es/group/microsoft.public.dotnet.languages.csharp/browse_th
read/thread/5445fa8ab5aa8928


You are right, I will have to use a separate property of type string as a
serialization holder and use System.Windows.Media.Color.ToString() to set
it. The ToArgb() method doesn't exist in System.Windows.Media.Color.

> I am serializing a System.Drawing.Color value by serializing the
> string representation, and then using a property of type Color that
> uses a converter to convert to and from the string.

The funny thing is is that System.Drawing.Color serializes "automatically"
without the need for a separate representation, as can be seen in the code I
sent in my original message to the C# group above. It really would be nice
to know if there is a reason why System.Drawing.Color can serialize when
System.Windows.Media.Color cannot or if it is an oversight that will be
modified in a future .net framework update.

> Just wondering.

Thanks again, RobinS, much appreciated!

Signature

Thank you,

Christopher Ireland
http://shunyata-kharg.doesntexist.com

"I went to a restaurant that serves 'breakfast at any time.' So I ordered
French Toast during the Renaissance."
Steven Wright

RobinS - 12 Mar 2008 04:37 GMT
> RobinS,
>
[quoted text clipped - 25 lines]
>
> Thanks again, RobinS, much appreciated!

Hmmm. That is really odd. Have you looked at the
System.Windows.Media.ColorConverter class to see if you can convert it to
and from system.windows.color or something usable? Of course, that's not far
off the mark from storing rgba values.

You might check and see if there is a Microsoft Forum for drawing and post
there and see if anyone from MSFT answers. They seem to appear more in the
MSDN forums than they do here (unless you're paying for support).

Good luck!
RobinS.
GoldMail.com
Christopher Ireland - 12 Mar 2008 10:18 GMT
RobinS,

Thanks again for your time!

> Hmmm. That is really odd. Have you looked at the
> System.Windows.Media.ColorConverter class to see if you can convert
> it to and from system.windows.color or something usable? Of course,
> that's not far off the mark from storing rgba values.

No, and whichever alternative way one chooses to represent
System.Windows.Media.Color this still means writing an extra public property
to store the value (public so it serializes correctly).

> You might check and see if there is a Microsoft Forum for drawing and
> post there and see if anyone from MSFT answers. They seem to appear
> more in the MSDN forums than they do here (unless you're paying for
> support).

That's where I posted first and didn't receive a reply. I've tried to check
just now whether anybody has answered but forums.microsoft.com is very slow
indeed at loading into my browser <sigh>.

Signature

Thank you,

Christopher Ireland
http://shunyata-kharg.doesntexist.com

"If he cannot stop the mind that seeks after fame and profit, he will spend
his life without finding peace."
Shobogenzo Zuimonki

Bob Powell [MVP] - 11 Mar 2008 21:45 GMT
Color is often serialized with a Memento Pattern implementation.

There are a couple of articles on serializing Color to XML on the web
which are easy enough to find on google.

Signature

Bob Powell [MVP]
Visual C#, System.Drawing

Ramuseco Limited .NET consulting
http://www.ramuseco.com

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.

> Hello!
>
[quoted text clipped - 8 lines]
>
> Thank you very much for any answers!
Christopher Ireland - 12 Mar 2008 09:59 GMT
Bob,

Many thanks for your reply!

> Color is often serialized with a Memento Pattern implementation.

Which color structure are you referring to here, System.Drawing.Color or
System.Windows.Media.Color? System.Drawing.Color requires no extra
implementation to serialize, as it is already marked with the Serializable
attribute.

> There are a couple of articles on serializing Color to XML on the web
> which are easy enough to find on google.

As I say, this isn't necessary for System.Drawing.Color and for
System.Windows.Media.Color this means adding in a publicly visible memento
in order for it to be serialized correctly. This memento could be of type
int or string, and the string could be a hex representation or xml; the type
of the memento pales into insignificance compared with the issues that
firstly it has to be written at all and secondly that it has to be public!!

:-(

Signature

Thank you,

Christopher Ireland
http://shunyata-kharg.doesntexist.com

"Egotism is the anesthetic that dulls the pain of stupidity."
Frank Leahy

Bob Powell [MVP] - 14 Mar 2008 09:25 GMT
I was referring to the fact that a similar problem with System.Drawing.Color
is solved in a specific way and that a similar solution may work for you
regarding System.Windows.Media.Color.

I don't understand your statement:

>> the type
of the memento pales into insignificance compared with the issues that
firstly it has to be written at all and secondly that it has to be public!!

While I agree that an oversight in the serialization system is annoying, a
memento pattern uses a different class to perform the serialization with a
representation that is able to describe the otherwise unserializable object.
or your purposes you could leave this memnto object internal...

Signature

--
Bob Powell [MVP]
Visual C#, System.Drawing

Ramuseco Limited .NET consulting
http://www.ramuseco.com

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.

> Bob,
>
[quoted text clipped - 19 lines]
>
> :-(
Christopher Ireland - 14 Mar 2008 09:49 GMT
Bob,

Thanks again for your time!

> I was referring to the fact that a similar problem with
> System.Drawing.Color is solved in a specific way and that a similar
> solution may work for you regarding System.Windows.Media.Color.

I've never had to implement any extra code to get System.Drawing.Color to
serialize correctly, but I agree with you that the only way to get
System.Windows.Media.Color to serialize is to serialize a string or int
representation of it.

> While I agree that an oversight in the serialization system is
> annoying, a memento pattern uses a different class to perform the
> serialization with a representation that is able to describe the
> otherwise unserializable object. or your purposes you could leave
> this memnto object internal...

I implement ISerializable on the object I'm serializing and do the
serialization by hand, as it were. So, imagine we have a public property of
the type System.Windows.Media.Color on this object; this property will have
to be marked with DesignerSerializationVisibility.Hidden to stop the
exception being thrown. As far as I can see, any alternative representation
will have to end up being another public property on this object because to
serialize the object I use TypeDescriptor.GetProperties() to get the
PropertyDescriptorCollection which I then iterate through adding the .net
framework types to SerializationInfo, and GetProperties() only returns
public properties.

I don't know how common the above scenario is or whether a memento internal
object is going to be of use within it.

Signature

Thank you,

Christopher Ireland
http://shunyata-kharg.doesntexist.com

"What is the color of wind?"
Zen Koan


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.