Example #1
0
        /// <summary>
        /// Creates an animated gif shape.  
        /// Do not add a very large number of these or performance may be degraded.
        /// </summary>
        /// <param name="imageName">
        /// The animated gif file (local or network) to load.
        /// </param>
        /// <param name="repeat">
        /// Continuously repeat the animation "True" or "False".
        /// </param>
        /// <returns>
        /// The animated gif shape name.
        /// </returns>
        public static Primitive AddAnimatedGif(Primitive imageName, Primitive repeat)
        {
            if (((string)imageName).StartsWith("http")) imageName = Network.DownloadFile(imageName);
            GraphicsWindow.Show();
            Type ShapesType = typeof(Shapes);
            Canvas _mainCanvas;
            string shapeName;

            try
            {
                MethodInfo method = GraphicsWindowType.GetMethod("VerifyAccess", BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.IgnoreCase);
                method.Invoke(null, new object[] { });

                method = ShapesType.GetMethod("GenerateNewName", BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.IgnoreCase);
                shapeName = method.Invoke(null, new object[] { "Image" }).ToString();

                _mainCanvas = (Canvas)GraphicsWindowType.GetField("_mainCanvas", BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.IgnoreCase).GetValue(null);

                InvokeHelperWithReturn ret = new InvokeHelperWithReturn(delegate
                {
                    try
                    {
                        System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(imageName);
                        System.Drawing.Imaging.FrameDimension fd = new System.Drawing.Imaging.FrameDimension(bitmap.FrameDimensionsList[0]);
                        int frameCount = bitmap.GetFrameCount(fd);

                        if (frameCount > 1)
                        {
                            Animated anim = new Animated();
                            animated.Add(anim);
                            anim.name = shapeName;
                            anim.frames = new Frame[frameCount];
                            anim.repeat = repeat;

                            //0x5100 is the property id of the GIF frame's durations
                            //this property does not exist when frameCount <= 1
                            byte[] times = bitmap.GetPropertyItem(0x5100).Value;

                            for (int i = 0; i < frameCount; i++)
                            {
                                //selects GIF frame based on FrameDimension and frameIndex
                                bitmap.SelectActiveFrame(fd, i);

                                //length in milliseconds of display duration
                                int length = BitConverter.ToInt32(times, 4 * i) * 10;

                                System.IO.MemoryStream stream = new System.IO.MemoryStream();
                                new System.Drawing.Bitmap(bitmap).Save(stream, System.Drawing.Imaging.ImageFormat.Png);

                                BitmapImage bi = new BitmapImage();
                                bi.BeginInit();
                                bi.StreamSource = stream;
                                bi.EndInit();

                                anim.frames[i] = new Frame(length, bi);
                            }

                            Image shape = new Image();
                            shape.Source = anim.frames[0].bi;
                            shape.Stretch = Stretch.Fill;
                            shape.Name = shapeName;
                            shape.Width = shape.Source.Width;
                            shape.Height = shape.Source.Height;
                            anim.shape = shape;

                            if (null == animationTimer)
                            {
                                animationTimer = new System.Windows.Forms.Timer();
                                animationTimer.Enabled = animationInterval > 0;
                                if (animationInterval > 0) animationTimer.Interval = animationInterval;
                                animationTimer.Tick += new System.EventHandler(animation_Tick);
                            }

                            _objectsMap[shapeName] = shape;
                            _mainCanvas.Children.Add(shape);
                            return shapeName;
                        }
                        return "";
                    }
                    catch (Exception ex)
                    {
                        Utilities.OnError(Utilities.GetCurrentMethod(), ex);
                        return "";
                    }
                });
                return FastThread.InvokeWithReturn(ret).ToString();
            }
            catch (Exception ex)
            {
                Utilities.OnError(Utilities.GetCurrentMethod(), ex);
                return "";
            }
        }
Example #2
0
        /// <summary>
        /// Creates an animation from a single image with multiple images on one layer.  
        /// Do not add a very large number of these or performance may be degraded.
        /// </summary>
        /// <param name="imageName">
        /// The image file (local or network) to load.
        /// Can also be an ImageList image.
        /// </param>
        /// <param name="repeat">
        /// Continuously repeat the animation "True" or "False".
        /// </param>
        /// <param name="countX">
        /// The number of sub-images in the X direction.
        /// </param>
        /// <param name="countY">
        /// The number of sub-images in the Y direction.
        /// </param>
        /// <returns>
        /// The animated shape name.
        /// </returns>
        public static Primitive AddAnimatedImage(Primitive imageName, Primitive repeat, Primitive countX, Primitive countY)
        {
            GraphicsWindow.Show();
            Type ShapesType = typeof(Shapes);
            Type ImageListType = typeof(ImageList);
            Dictionary<string, BitmapSource> _savedImages;
            BitmapSource img;
            Canvas _mainCanvas;
            string shapeName;

            try
            {
                _savedImages = (Dictionary<string, BitmapSource>)ImageListType.GetField("_savedImages", BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.IgnoreCase).GetValue(null);
                if (!_savedImages.TryGetValue((string)imageName, out img))
                {
                    imageName = ImageList.LoadImage(imageName);
                    if (!_savedImages.TryGetValue((string)imageName, out img))
                    {
                        return "";
                    }
                }

                MethodInfo method = GraphicsWindowType.GetMethod("VerifyAccess", BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.IgnoreCase);
                method.Invoke(null, new object[] { });

                method = ShapesType.GetMethod("GenerateNewName", BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.IgnoreCase);
                shapeName = method.Invoke(null, new object[] { "Image" }).ToString();

                _mainCanvas = (Canvas)GraphicsWindowType.GetField("_mainCanvas", BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.IgnoreCase).GetValue(null);

                InvokeHelperWithReturn ret = new InvokeHelperWithReturn(delegate
                {
                    try
                    {
                        //System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(imageName);
                        System.Drawing.Bitmap bitmap;
                        using (MemoryStream outStream = new MemoryStream())
                        {
                            BitmapEncoder enc = new PngBitmapEncoder();
                            enc.Frames.Add(BitmapFrame.Create(img));
                            enc.Save(outStream);
                            bitmap = new System.Drawing.Bitmap(outStream);
                        }

                        int frameCount = countX * countY;

                        if (frameCount > 1)
                        {
                            Animated anim = new Animated();
                            animated.Add(anim);
                            anim.name = shapeName;
                            anim.frames = new Frame[frameCount];
                            anim.repeat = repeat;

                            int w = bitmap.Width / countX;
                            int h = bitmap.Height / countY;

                            for (int j = 0; j < countY; j++)
                            {
                                for (int i = 0; i < countX; i++)
                                {
                                    System.Drawing.RectangleF cloneRect = new System.Drawing.RectangleF(w * i, h * j, w, h);
                                    System.Drawing.Bitmap crop = bitmap.Clone(cloneRect, bitmap.PixelFormat);

                                    System.IO.MemoryStream stream = new System.IO.MemoryStream();
                                    new System.Drawing.Bitmap(crop).Save(stream, System.Drawing.Imaging.ImageFormat.Png);

                                    BitmapImage bi = new BitmapImage();
                                    bi.BeginInit();
                                    bi.StreamSource = stream;
                                    bi.EndInit();

                                    anim.frames[j * countX + i] = new Frame(0, bi);
                                }
                            }

                            Image shape = new Image();
                            shape.Source = anim.frames[0].bi;
                            shape.Stretch = Stretch.Fill;
                            shape.Name = shapeName;
                            shape.Width = shape.Source.Width;
                            shape.Height = shape.Source.Height;
                            anim.shape = shape;

                            if (null == animationTimer)
                            {
                                animationTimer = new System.Windows.Forms.Timer();
                                animationTimer.Enabled = animationInterval > 0;
                                if (animationInterval > 0) animationTimer.Interval = animationInterval;
                                animationTimer.Tick += new System.EventHandler(animation_Tick);
                            }

                            _objectsMap[shapeName] = shape;
                            _mainCanvas.Children.Add(shape);
                            return shapeName;
                        }
                        return "";
                    }
                    catch (Exception ex)
                    {
                        Utilities.OnError(Utilities.GetCurrentMethod(), ex);
                        return "";
                    }
                });
                return FastThread.InvokeWithReturn(ret).ToString();
            }
            catch (Exception ex)
            {
                Utilities.OnError(Utilities.GetCurrentMethod(), ex);
                return "";
            }
        }
Example #3
0
 /// <summary>
 /// Reset animated image to a selected image.
 /// </summary>
 /// <param name="shapeName">
 /// The animated image shape name.
 /// </param>
 /// <param name="image">
 /// The selected animated image number (indexed from 1).
 /// </param>
 public static void AnimationSet(Primitive shapeName, Primitive image)
 {
     foreach (Animated anim in animated)
     {
         if (anim.name == shapeName)
         {
             anim.iFrame = image - 2;
             delegateAnim = anim;
             FastThread.Invoke(updateGif_Delegate);
         }
     }
 }