I have retried images from a server and added a mouse click event upon which I add the image to a new canvas
//Code
if (e.Result != null)
{
BitmapImage bitmapImage = new BitmapImage();
PictureFile pict = new PictureFile();
pict.PictureStream = e.Result;
MemoryStream stream = new MemoryStream(pict.PictureStream);
bitmapImage.SetSource(stream);
Image image = new Image();
image.SetValue(Image.SourceProperty, bitmapImage);
image.SetValue(Image.NameProperty, (imagename.Replace(".jpg","")));
//image.SetValue(Image.SourceProperty, img);
image.SetValue(Image.WidthProperty, (double)45);
image.SetValue(Image.HeightProperty, (double)45);
image.MouseMove += new MouseEventHandler(Element_MouseMove);
image.MouseLeftButtonDown += new MouseButtonEventHandler(Element_MouseLeftButtonDown);
image.MouseLeftButtonUp += new MouseButtonEventHandler(Element_MouseLeftButtonUp);
}
When I tried to add the same image second time to this canvas it shows the error. The reason is the same name can't be used for different Image object.I have to save this canvas as xaml(which I can do now).Once I add the images I must be able to remap them
to the original file.
Can anyone suggest a solution
I am a bit new to silverlight so cant find a solution.Thanks in advance
You cannot add the same UIElement to the layout more than once. Image is a UIElement, so that's why you are getting an error. You can however reuse the BitmapSource, so if you want to add the image multiple times, simply run this code once again:
Also, you don't really need to set the Name property. This is really only used when you declare it in XAML and want to reference it in code. However in this case, you declare it in code, and already have a reference to it (ie. the "image" variable).
--
/Morten | Silverlight MVP | blog - twitter Please click on "Mark as Answer" if this answered your question.
"I have to specify the name so that I know which I am refering. On reloading canvas I will know the source.."
Again, the xaml name is ONLY for referring elements present in XAML from codebehind. In this case you are 100% in codebehind, and you are creating the reference yourself. Instead of just having that reference inside your method, make it a variable at the
class level, so you can reference it later (this is basically what x:Name does under the covers).
--
/Morten | Silverlight MVP | blog - twitter Please click on "Mark as Answer" if this answered your question.
Ajo Mathew
Member
36 Points
50 Posts
copying image from one canvas to another
Mar 24, 2011 02:09 PM | LINK
I have retried images from a server and added a mouse click event upon which I add the image to a new canvas
//Code
if (e.Result != null) { BitmapImage bitmapImage = new BitmapImage(); PictureFile pict = new PictureFile(); pict.PictureStream = e.Result; MemoryStream stream = new MemoryStream(pict.PictureStream); bitmapImage.SetSource(stream); Image image = new Image(); image.SetValue(Image.SourceProperty, bitmapImage); image.SetValue(Image.NameProperty, (imagename.Replace(".jpg",""))); //image.SetValue(Image.SourceProperty, img); image.SetValue(Image.WidthProperty, (double)45); image.SetValue(Image.HeightProperty, (double)45); image.MouseMove += new MouseEventHandler(Element_MouseMove); image.MouseLeftButtonDown += new MouseButtonEventHandler(Element_MouseLeftButtonDown); image.MouseLeftButtonUp += new MouseButtonEventHandler(Element_MouseLeftButtonUp); }When I tried to add the same image second time to this canvas it shows the error. The reason is the same name can't be used for different Image object.I have to save this canvas as xaml(which I can do now).Once I add the images I must be able to remap them to the original file.
Can anyone suggest a solution
I am a bit new to silverlight so cant find a solution.Thanks in advance
SharpGIS
Contributor
4825 Points
832 Posts
Re: copying image from one canvas to another
Mar 25, 2011 03:41 AM | LINK
You cannot add the same UIElement to the layout more than once. Image is a UIElement, so that's why you are getting an error. You can however reuse the BitmapSource, so if you want to add the image multiple times, simply run this code once again:
Image image2 = new Image();
image2.SetValue(Image.SourceProperty, bitmapImage);
image2.SetValue(Image.WidthProperty, (double)45);
image2.SetValue(Image.HeightProperty, (double)45);
image2.MouseMove += new MouseEventHandler(Element_MouseMove);
image2.MouseLeftButtonDown += new MouseButtonEventHandler(Element_MouseLeftButtonDown);
image2.MouseLeftButtonUp += new MouseButtonEventHandler(Element_MouseLeftButtonUp);
Also, you don't really need to set the Name property. This is really only used when you declare it in XAML and want to reference it in code. However in this case, you declare it in code, and already have a reference to it (ie. the "image" variable).
/Morten | Silverlight MVP | blog - twitter
Please click on "Mark as Answer" if this answered your question.
Ajo Mathew
Member
36 Points
50 Posts
Re: Re: copying image from one canvas to another
Mar 25, 2011 04:55 AM | LINK
I have to specify the name so that I know which I am refering. On reloading canvas I will know the source..
I did this saved the source images name in the tag property.. I will try your method.. Thanks for replaying
SharpGIS
Contributor
4825 Points
832 Posts
Re: Re: copying image from one canvas to another
Mar 26, 2011 04:44 AM | LINK
"I have to specify the name so that I know which I am refering. On reloading canvas I will know the source.."
Again, the xaml name is ONLY for referring elements present in XAML from codebehind. In this case you are 100% in codebehind, and you are creating the reference yourself. Instead of just having that reference inside your method, make it a variable at the class level, so you can reference it later (this is basically what x:Name does under the covers).
/Morten | Silverlight MVP | blog - twitter
Please click on "Mark as Answer" if this answered your question.