Beispiel #1
0
        public static Bitmap LoadCustomImageFromCache(string sFile, ProjectLayoutElement zElement, Color colorOverride, int nTargetWidth = -1, int nTargetHeight = -1)
        {
            var sKey = sFile.ToLower() + ":" + zElement.opacity + ":" + nTargetWidth + ":" + nTargetHeight + ProjectLayoutElement.GetElementColorString(colorOverride);

            if (s_dictionaryCustomImages.TryGetValue(sKey, out var zDestinationBitmap))
            {
                return(zDestinationBitmap);
            }

            var zElementType = EnumUtil.GetElementType(zElement.type);

            var zSourceBitmap = LoadImageFromCache(sFile);

            if (null == zSourceBitmap)
            {
                return(null);
            }
            // if the desired width/height/opacity match the 'plain' cached copy just return it (or special color handling for certain element types)
            // TODO: make a method for this just to shrink all this logic down
            if (
                (
                    (-1 == nTargetWidth || zSourceBitmap.Width == nTargetWidth) &&
                    (-1 == nTargetHeight || zSourceBitmap.Height == nTargetHeight) &&
                    255 == zElement.opacity
                )
                )
            {
                switch (zElementType)
                {
                case ElementType.FormattedText:
                case ElementType.Graphic:
                    if (colorOverride == Color.Black)
                    {
                        return(zSourceBitmap);
                    }
                    break;

                default:
                    return(zSourceBitmap);
                }
            }
            // TODO: should this be handled in a shared way?
            // TODO: this is a terrible eviction strategy
            if (s_dictionaryCustomImages.Count > IMAGE_CACHE_MAX)
            {
                DumpImagesFromDictionary(s_dictionaryCustomImages);
            }

            var zImageAttributes = new ImageAttributes();
            var zColor           = new ColorMatrix();

            if (255 != zElement.opacity)
            {
                zColor.Matrix33 = (float)zElement.opacity / 255.0f;
            }
            // special color handling for certain element types
            if (colorOverride != Color.Black)
            {
                switch (zElementType)
                {
                case ElementType.FormattedText:
                case ElementType.Graphic:
                    zColor.Matrix40 = (float)colorOverride.R / 255.0f;
                    zColor.Matrix41 = (float)colorOverride.G / 255.0f;
                    zColor.Matrix42 = (float)colorOverride.B / 255.0f;
                    break;
                }
            }
            zImageAttributes.SetColorMatrix(zColor);

            nTargetWidth  = nTargetWidth == -1 ? zSourceBitmap.Width : nTargetWidth;
            nTargetHeight = nTargetHeight == -1 ? zSourceBitmap.Height : nTargetHeight;

            zDestinationBitmap = new Bitmap(nTargetWidth, nTargetHeight); // target image
            var zGraphics = Graphics.FromImage(zDestinationBitmap);

            // draw the source image into the destination with the desired opacity
            zGraphics.DrawImage(zSourceBitmap, new Rectangle(0, 0, nTargetWidth, nTargetHeight), 0, 0, zSourceBitmap.Width, zSourceBitmap.Height, GraphicsUnit.Pixel,
                                zImageAttributes);
            CacheImage(s_dictionaryCustomImages, sKey, zDestinationBitmap);

            return(zDestinationBitmap);
        }