source: trunk/CrypPlugins/WorkspaceManager/Model/ImageModel.cs @ 2280

Last change on this file since 2280 was 1895, checked in by kopal, 11 years ago
  • removed Parent because it did not work well (have to implement new Parent concept)
  • TextModels (RichTextBox) data is now saved/loaded
File size: 2.4 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using System.Windows.Media.Imaging;
6using System.IO;
7using System.Windows.Media;
8using System.Windows.Controls;
9
10namespace WorkspaceManager.Model
11{
12    /// <summary>
13    /// This class wraps a image which can be put onto the workspace
14    /// </summary>
15    [Serializable]
16    public class ImageModel : VisualElementModel
17    {
18        private byte[] data = null;
19
20        /// <summary>
21        /// Get the Image stored by this ImageModel
22        /// </summary>
23        /// <returns></returns>
24        public Image getImage()
25        {
26            Image image = new Image();           
27            if (data == null)
28            {
29                return image;
30            }
31
32            MemoryStream stream = new MemoryStream(this.data);
33            JpegBitmapDecoder decoder = new JpegBitmapDecoder(stream, BitmapCreateOptions.None, BitmapCacheOption.Default);
34            BitmapFrame frame = decoder.Frames.First();
35            image.Source = frame;           
36            return image;
37        }
38
39        /// <summary>
40        /// Instantiate a new ImageModel
41        /// </summary>
42        public ImageModel()           
43        {
44
45        }
46        /// <summary>
47        /// Instantiate a new ImageModel
48        /// Loads the image from the imgUri and converts it into a jpeg
49        /// Afterwards the data are stored in an internal byte array
50        /// </summary>
51        /// <param name="imageSource"></param>
52        public ImageModel(Uri imgUri)
53        {
54            if (imgUri == null)
55            {
56                return;
57            }
58            Width = 0;
59            Height = 0;
60            BitmapImage bmpImage = new BitmapImage(imgUri) ;
61            JpegBitmapEncoder encoder = new JpegBitmapEncoder();
62            encoder.Frames.Add(BitmapFrame.Create(bmpImage));
63            MemoryStream stream = new MemoryStream();
64            encoder.Save(stream);
65            this.data = stream.ToArray();
66            stream.Close();
67        }
68
69        /// <summary>
70        /// The WorkspaceModel of this ImageModel
71        /// </summary>
72        public WorkspaceModel WorkspaceModel { get; set; }
73
74        /// <summary>
75        /// is the image enabled ?
76        /// </summary>
77        private bool isEnabled = true;
78        public bool IsEnabled { get { return isEnabled; } set { isEnabled = value; } }
79    }
80}
Note: See TracBrowser for help on using the repository browser.