Is Picture Shape Support gif Image (dynamic pictures )?

Jul 24, 2012 at 7:11 AM

In demo, Picture Shape can load gif file,but can't show dynamic Image.

Jul 25, 2012 at 9:23 AM
Edited Jul 25, 2012 at 9:23 AM

Hello dragonren.

In order to show a dynamically created image, you have to force the shape to completely recalculate its draw cache. This is best done by setting the image to null/Nothing, draw your image content and set the drawn image again.

Here is a small sample:
I've added a timer (Interval: 1 second) to my demo project. In the timer's Tick event handler, the current time is drawn into the image of the picture shape.

        Picture m_PicShape = null;

        private void timer1_Tick(object sender, EventArgs e) {
            // Create a picture shape if it does not exist yet
            if (m_PicShape == null) {
                m_PicShape = project1.ShapeTypes["Picture"].CreateInstance() as Picture;
                // Create a bitmap image
                Bitmap img = new Bitmap(m_PicShape.Width, m_PicShape.Height);
                m_PicShape.Image = new NamedImage(img, "Demo Bitmap");
                m_PicShape.X = display1.Diagram.Width / 2;
                m_PicShape.Y = display1.Diagram.Height / 2;
                // Add the shape
            // Create dynamic image content
            if (m_PicShape.Image.Image != null) {
                // Store the current image
                NamedImage img = m_PicShape.Image;
                // Set picture shape's image to null in order to force a redraw
                m_PicShape.Image = null;
                // Draw new content into the image
                using (Graphics gfx = Graphics.FromImage(img.Image)) {
                    string txt = DateTime.Now.ToString("HH:mm:ss");
                    Rectangle r = Rectangle.Empty;
                    r.Size = TextMeasurer.MeasureText(txt, Font, Size.Empty, m_PicShape.ParagraphStyle);
                    r.X = (img.Width - r.Width) / 2;
                    r.Y = (img.Height - r.Height) / 2;

                    gfx.DrawString(txt, Font, Brushes.Navy, r);
                // Assign the image to the shape again
                m_PicShape.Image = img;

Hope this helps.