예제 #1
0
 public DrawingSession(UniversalBitmap owner)
 {
     this.Parent       = owner;
     this.pixelBuffer  = owner.pixelBuffer ?? new PixelBuffer(owner.bitmap, owner.Width, owner.Height);
     owner.pixelBuffer = this.pixelBuffer;
     this.pixelBuffer.Reset();
 }
예제 #2
0
        public byte[] GenerateIconFile()
        {
            int[] sizes = this.bitmaps.Keys.OrderBy(i => i).ToArray();
            if (sizes.Length == 0)
            {
                throw new InvalidOperationException("Cannot generate empty icon file.");
            }
            List <byte> imageBytes        = new List <byte>();
            List <int>  startingPositions = new List <int>();
            int         startingPosition  = 6 + 16 * sizes.Length; // ico header is 6 bytes and each file header is 16 bytes.
            List <byte> icoHeader         = new List <byte>();
            List <byte> pngHeaders        = new List <byte>();
            List <byte> pngPayloads       = new List <byte>();

            ToLittleEndian(0, 2, icoHeader);            // first two bytes are always 0
            ToLittleEndian(1, 2, icoHeader);            // 1 for ICO format (2 is CUR)
            ToLittleEndian(sizes.Length, 2, icoHeader); // number of files

            foreach (int size in sizes)
            {
                UniversalBitmap originalImage    = this.bitmaps[size];
                int             width            = originalImage.Width;
                int             height           = originalImage.Height;
                int             x                = (size - width) / 2;
                int             y                = (size - height) / 2;
                UniversalBitmap resource         = new UniversalBitmap(size, size);
                UniversalBitmap.DrawingSession g = resource.CreateNewDrawingSession();

                g.Blit(originalImage, x, y);
                g.Flush();

                byte[] pngBytes = resource.GetBytesAsPng();
                pngPayloads.AddRange(pngBytes);

                ToLittleEndian(size == 256 ? 0 : size, 1, pngHeaders);
                ToLittleEndian(size == 256 ? 0 : size, 1, pngHeaders);
                ToLittleEndian(0, 1, pngHeaders);                // 0 for not using a color palette
                ToLittleEndian(0, 1, pngHeaders);                // reserved, always 0
                ToLittleEndian(0, 2, pngHeaders);                // 0 color planes.
                ToLittleEndian(32, 2, pngHeaders);               // 32 bits per pixel
                ToLittleEndian(pngBytes.Length, 4, pngHeaders);  // file size in bytes
                ToLittleEndian(startingPosition, 4, pngHeaders); // byte position from the beginning of the file

                startingPosition += pngBytes.Length;
            }

            List <byte> finalOutput = icoHeader;

            finalOutput.AddRange(pngHeaders);
            finalOutput.AddRange(pngPayloads);

            return(finalOutput.ToArray());
        }
예제 #3
0
        public UniversalBitmap CloneToNewSize(int width, int height)
        {
            UniversalBitmap newBitmap = new UniversalBitmap(width, height);
            DrawingSession  g         = newBitmap.CreateNewDrawingSession();

            if (width == this.Width && height == this.Height)
            {
                g.Blit(this, 0, 0);
            }
            else
            {
                g.BlitStretched(this, 0, 0, width, height);
            }
            g.Flush();
            newBitmap.activeDrawingSession = null;
            newBitmap.pixelBuffer          = null;
            return(newBitmap);
        }
예제 #4
0
 public DrawingSession Draw(
     UniversalBitmap bmp,
     int targetX, int targetY,
     int sourceX, int sourceY,
     int targetWidth, int targetHeight,
     int sourceWidth, int sourceHeight)
 {
     if (sourceX == 0 && sourceY == 0 && sourceWidth == bmp.Width && sourceHeight == bmp.Height)
     {
         // attempting to draw the whole src onto the dest
         this.BlitStretched(bmp, targetX, targetY, targetWidth, targetHeight);
     }
     else
     {
         throw new System.NotImplementedException();
     }
     return(this);
 }
예제 #5
0
        public void AddImage(UniversalBitmap bmp)
        {
            int width   = bmp.Width;
            int height  = bmp.Height;
            int largest = System.Math.Max(width, height);

            if (width > 256 || height > 256)
            {
                throw new InvalidOperationException("Icon images cannot be larger than 256 x 256");
            }
            foreach (int size in new int[] { 256, 128, 64, 32 })
            {
                if (largest > size / 2)
                {
                    this.bitmaps[size] = bmp;
                    return;
                }
            }
            this.bitmaps[16] = bmp;
        }
예제 #6
0
 public void BlitStretched(UniversalBitmap bmp, int x, int y, int stretchWidth, int stretchHeight)
 {
     this.pixelBuffer.BlitStretched(this.Parent.bitmap, bmp.bitmap, x, y, stretchWidth, stretchHeight);
 }
예제 #7
0
 public void Blit(UniversalBitmap bmp, int x, int y)
 {
     this.BlitStretched(bmp, x, y, bmp.Width, bmp.Height);
 }