Help with Custom Format & Clipboard

B

bern11

I have an data structure with a bitmap as a member. I want to be able
to copy the object to the clipboard and paste it back into my app as a
new object, or into another app as a bitmap. It looks like the
dataobject class does this, but I'm not exactly sure how to set it up.

1) Should I just make a DataObject and call SetData twice, once with a
Bitmap object and once with a custom object, or 2) should I inherit from
dataobject and overload? 1st one sounds easier.
 
N

Nicholas Paldino [.NET/C# MVP]

I believe if your structure/class is serializable, the DataObject class
will support placing it on the clipboard. The Bitmap class is serilizable,
so if your struct/class is serializable, then you can just add it as one
item.
 
B

bern11

That is where I get confused. Do I declare it [Serializeable] and
implement the interfaces or inherit from DataObject and override?

The only example I've seen creates a DataObject and uses SetData
implicitly through the constructor.
 
N

Nicholas Paldino [.NET/C# MVP]

You want to add the Serializable attribute to your type, and then pass
that type to SetData on a DataObject. You don't want to have to implement
IDataObject (or extend DataObject), you want to make the calls to
DataObject, passing your object.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

bern11 said:
That is where I get confused. Do I declare it [Serializeable] and
implement the interfaces or inherit from DataObject and override?

The only example I've seen creates a DataObject and uses SetData
implicitly through the constructor.
I believe if your structure/class is serializable, the DataObject
class will support placing it on the clipboard. The Bitmap class is
serilizable, so if your struct/class is serializable, then you can just
add it as one item.
 
B

bern11

I'm 1/2-way there. I can put the object on the clipboard and detect it
with, I just can't get it back. I can paste the bitmap, just not the
object:

DataObject copyObject = new DataObject();
DataFormats.Format myFormat = DataFormats.GetFormat("myCustomType");

copyObject.SetData(DataFormats.Bitmap, myClassObject.bitmap);
copyObject.SetData("myCustomType", myClassObject);
Clipboard.SetDataObject(copyObject);

....
if (Clipboard.ContainsData("myCustomType")) {
??? = Clipboard.GetData("myCustomType");

....
// Class declared like this...
[Serializable]
public class myClassObject
{
Bitmap bitmap;
... other fields
}
 
B

bern11

I figured out the problem: One of my fields was an ImageAttributes data
type, and it didn't like being serialized. I set it to NonSerialzed and
everything worked. It was kindof annoying that my program compiled &
ran wihtout throwing an error, but it just didn't work... Whats up with
that?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top