Reputation: 3405
I am getting an error by implementing this simple code. I donot understand where I am doing mistake.
// ERROR
An unhandled exception of type 'System.NullReferenceException' occurred in ImageCSharp.exe
Additional information: Der Objektverweis wurde nicht auf eine Objektinstanz festgelegt.
I can get clipboard text but why i can't get /set image.
//CODE
public void copy()
{
// Determine the active child form.
fImage activeChild = this.ActiveMdiChild as fImage;
if (activeChild != null)
{
PictureBox mypicbox = activeChild.picbox;
string win_name = activeChild.Tag.ToString();
Clipboard.SetImage(mypicbox.Image);
Clipboard.SetText(win_name);
}
}
private void paste()
{
Image im= Clipboard.GetImage();
this.pictureBox1.Image = im;
MessageBox.Show(im.Size.ToString());
}
regards,
Upvotes: 0
Views: 982
Reputation: 941218
Clipboard.SetText(win_name);
That dumps the image off the clipboard, it can only hold one item. Delete the line to solve your problem. And code defensively:
private void paste() {
if (Clipboard.ContainsImage()) {
Image im = Clipboard.GetImage();
if (this.pictureBox1.Image != null) this.pictureBox1.Dispose();
this.pictureBox1.Image = im;
}
}
To get both pieces of info on the clipboard, first declare a little helper class to store this info. For example:
[Serializable]
private class Clipdata {
public Image Image { get; set; }
public string Name { get; set; }
}
private void CopyButton_Click(object sender, EventArgs e) {
var data = new Clipdata { Image = pictureBox1.Image, Name = textBox1.Text };
Clipboard.SetDataObject(data);
}
private void PasteButton_Click(object sender, EventArgs e) {
string fmt = typeof(Clipdata).FullName;
if (Clipboard.ContainsData(fmt)) {
var data = (Clipdata)Clipboard.GetDataObject().GetData(fmt);
if (pictureBox1.Image != null) pictureBox1.Image.Dispose();
pictureBox1.Image = data.Image;
textBox1.Text = data.Name;
}
}
Upvotes: 1
Reputation: 33476
Not an answer to your question, but following won't set the image as well as text to the clipboard. i.e. your code will set the text to the clipboard
Clipboard.SetImage(mypicbox.Image);
Clipboard.SetText(win_name);
The above code is trying to set the image to the clipboard, followed by text.
i.e. clipboard will contain one item, which is text as per your code.
And I assume because of that the code inside paste
which expects an image to be in clipboard, is throwing exception on MessageBox.Show(img.Size.ToString());
.
Upvotes: 1