示例#1
0
        private void TakeScreenshot()
        {
            if (!this.GetVirtualMachine())
            {
                return;
            }

            this.LogTaskMessage(string.Format(CultureInfo.CurrentCulture, "Taking screenshot of: {0}", this.Name));
            VMDisplay display = this.virtualMachine.Display;

            if (display != null)
            {
                object   thumbnailObject = display.Thumbnail;
                object[] thumbnail       = (object[])thumbnailObject;
                using (Bitmap bmp = new Bitmap(64, 48, PixelFormat.Format32bppRgb))
                {
                    for (int y = 0; y < bmp.Height; y++)
                    {
                        for (int x = 0; x < bmp.Width; x++)
                        {
                            uint pixel = (uint)thumbnail[(y * bmp.Width) + x];

                            int b = (int)((pixel & 0xff000000) >> 24);
                            int g = (int)((pixel & 0x00ff0000) >> 16);
                            int r = (int)((pixel & 0x0000ff00) >> 8);

                            bmp.SetPixel(x, y, Color.FromArgb(r, g, b));
                        }
                    }

                    bmp.Save(this.FileName.ItemSpec);
                }
            }
        }
示例#2
0
        public static Bitmap GenerateThumbnail(VMDisplay vmdisplay)
        {
            Bitmap thumbnail = null;

            // Count total number of pixels of the thumbnail. Can I use 64x48x4 directly?
            int count = 0;

            foreach (Object image in (IEnumerable)vmdisplay.Thumbnail)
            {
                count++;
            }

            // Get a byte array to store the bitmap. Initial values are 0.
            // Every element has 4 bytes. Plus a 54 bytes Bitmap header.
            byte[] bitmapdata = new Byte[count * 4 + 54];


            // Bitmap header.
            bitmapdata[0]  = 66;
            bitmapdata[1]  = 77;
            bitmapdata[2]  = 54;
            bitmapdata[3]  = 36;
            bitmapdata[10] = 54;
            bitmapdata[14] = 40;
            bitmapdata[18] = 64;                // Image width
            bitmapdata[22] = 48;                // Image height
            bitmapdata[26] = 1;
            bitmapdata[28] = 24;                // Bit depth
            bitmapdata[35] = 36;

            int  pixIndex = 54;
            long pixel    = 0;

            foreach (Object image in (IEnumerable)vmdisplay.Thumbnail)
            {
                // Every pixel has 4 bytes. Ignore the last one.
                pixel = long.Parse(image.ToString());
                bitmapdata[pixIndex]     = byte.Parse((pixel & 4278190080) / 16777216 + "");
                bitmapdata[pixIndex + 1] = byte.Parse((pixel & 16711680) / 65536 + "");
                bitmapdata[pixIndex + 2] = byte.Parse((pixel & 65280) / 256 + "");

                pixIndex += 3;
            }

            System.IO.MemoryStream streamBitmap = new  System.IO.MemoryStream(bitmapdata);
            thumbnail = new Bitmap((Bitmap)Image.FromStream(streamBitmap));

            // Rotate and flip the image
            thumbnail.RotateFlip(RotateFlipType.Rotate180FlipX);

            return(thumbnail);
        }
        private void TakeScreenshot()
        {
            this.Success.Set(this.ActivityContext, false);
            this.LogBuildMessage(string.Format(CultureInfo.CurrentCulture, "Taking screenshot of: {0}", this.VMName.Get(this.ActivityContext)));

            if (string.IsNullOrEmpty(this.FileName.Get(this.ActivityContext)))
            {
                this.LogBuildError("No filename provided");
                return;
            }

            if (!this.GetVirtualMachine())
            {
                return;
            }

            VMDisplay display = this.virtualMachine.Display;

            if (display != null)
            {
                object   thumbnailObject = display.Thumbnail;
                object[] thumbnail       = (object[])thumbnailObject;
                using (Bitmap bmp = new Bitmap(64, 48, PixelFormat.Format32bppRgb))
                {
                    for (int y = 0; y < bmp.Height; y++)
                    {
                        for (int x = 0; x < bmp.Width; x++)
                        {
                            uint pixel = (uint)thumbnail[(y * bmp.Width) + x];

                            int b = (int)((pixel & 0xff000000) >> 24);
                            int g = (int)((pixel & 0x00ff0000) >> 16);
                            int r = (int)((pixel & 0x0000ff00) >> 8);

                            bmp.SetPixel(x, y, Color.FromArgb(r, g, b));
                        }
                    }

                    bmp.Save(this.FileName.Get(this.ActivityContext));
                    this.Success.Set(this.ActivityContext, true);
                }
            }
        }