예제 #1
0
        public static void CreateShadowImage(string fileName, int swidth)
        {
            Int32   shadowwidth = swidth;
            Int32   borderwidth = 0;
            Int32   margin      = shadowwidth;
            Int32   shadowdir   = 0;
            Double  shadowtrans = 0.0;
            Color   bkcolor     = Color.FromArgb(255, 255, 255);
            Color   shadowcolor = Color.FromArgb(0, 0, 0);
            Color   bordercolor = Color.FromArgb(0, 0, 0);
            Boolean softshadow  = true;

            String outputfile = fileName;
            String inputfile  = fileName;

            FastBitmap tmp, bmp;

            if (System.IO.File.Exists(inputfile))
            {
                tmp = new FastBitmap(inputfile);
            }
            else
            {
                Console.WriteLine("Error: Could not find file '{0}'", inputfile);
                return;
            }

            bmp = new FastBitmap(tmp.Width + borderwidth * 2, tmp.Height + borderwidth * 2, PixelFormat.Format32bppArgb);

            // add border if necessary
            if (borderwidth > 0)
            {
                SolidBrush br = new SolidBrush(bordercolor);
                Graphics   g  = Graphics.FromImage(bmp._bitmap);
                g.FillRectangle(br, 0, 0, borderwidth * 2 + tmp.Width, borderwidth * 2 + tmp.Height);
                g.Dispose();
                br.Dispose();
            }

            tmp.CopyTo(bmp, borderwidth, borderwidth, 0, 0, tmp.Width, tmp.Height);
            tmp.Dispose();

            // create image
            Int32        width  = bmp.Width + shadowwidth + margin * 2;
            Int32        height = bmp.Height + shadowwidth + margin * 2;
            LayeredImage image  = new LayeredImage(width, height);

            Int32 shadowx = 0, shadowy = 0, imgx = 0, imgy = 0;

            if (softshadow)
            {
                switch (shadowdir)
                {
                case 0:
                {
                    shadowx = margin - shadowwidth / 2;
                    shadowy = margin - shadowwidth / 2;
                    imgx    = margin;
                    imgy    = margin;
                    break;
                }

                case 1:
                {
                    shadowx = margin + shadowwidth - 3 * (shadowwidth / 2);
                    shadowy = margin - shadowwidth / 2;
                    imgx    = margin + shadowwidth;
                    imgy    = margin;
                    break;
                }

                case 2:
                {
                    shadowx = margin + shadowwidth - 3 * (shadowwidth / 2);
                    shadowy = margin + shadowwidth - 3 * (shadowwidth / 2);
                    imgx    = margin + shadowwidth;
                    imgy    = margin + shadowwidth;
                    break;
                }

                case 3:
                {
                    shadowx = margin - shadowwidth / 2;
                    shadowy = margin + shadowwidth - 3 * (shadowwidth / 2);
                    imgx    = margin;
                    imgy    = margin + shadowwidth;
                    break;
                }
                }
            }
            else
            {
                switch (shadowdir)
                {
                case 0:
                {
                    shadowx = margin;
                    shadowy = margin;
                    imgx    = margin;
                    imgy    = margin;
                    break;
                }

                case 1:
                {
                    shadowx = margin - shadowwidth;
                    shadowy = margin;
                    imgx    = margin + shadowwidth;
                    imgy    = margin;
                    break;
                }

                case 2:
                {
                    shadowx = margin - shadowwidth;
                    shadowy = margin - shadowwidth;
                    imgx    = margin + shadowwidth;
                    imgy    = margin + shadowwidth;
                    break;
                }

                case 3:
                {
                    shadowx = margin;
                    shadowy = margin - shadowwidth;
                    imgx    = margin;
                    imgy    = margin + shadowwidth;
                    break;
                }
                }
            }

            // background
            Layer bg = image.Layers.Add();

            bg.Clear(bkcolor);

            // shadow -- layer must be larger because of blur
            Layer      shadow = image.Layers.Add(width + shadowwidth, height + shadowwidth);
            SolidBrush brush  = new SolidBrush(shadowcolor);

            shadow.FillRectangle(shadowwidth, shadowwidth, bmp.Width, bmp.Height, brush);

            if (softshadow)
            {
                shadow.Blur(shadowwidth, shadowwidth);
            }

            brush.Dispose();
            shadow.OffsetX = shadowx;
            shadow.OffsetY = shadowy;
            shadow.Opacity = 1.0 - shadowtrans;

            // image
            Layer img = image.Layers.Add(bmp);

            img.OffsetX = imgx;
            img.OffsetY = imgy;

            // result
            FastBitmap result = image.Flatten();

            // save
            String filename = outputfile != "" ? outputfile : inputfile;
            String ext      = Path.GetExtension(filename);

            if (ext == "")
            {
                ext = ".bmp";
            }

            ext = ext.ToLower();
            ImageFormat imgf = ImageFormat.Bmp;

            switch (ext)
            {
            case ".bmp":
                ext  = ".bmp";
                imgf = ImageFormat.Bmp;
                break;

            case ".jpg":
                ext  = ".jpg";
                imgf = ImageFormat.Jpeg;
                break;

            case ".jpeg":
                ext  = ".jpeg";
                imgf = ImageFormat.Jpeg;
                break;

            case ".png":
                ext  = ".png";
                imgf = ImageFormat.Png;
                break;

            case ".gif":
                ext  = ".gif";
                imgf = ImageFormat.Gif;
                break;

            default:
                ext  = ".bmp";
                imgf = ImageFormat.Bmp;
                break;
            }

            result.Save(filename, imgf);
        }
예제 #2
0
 public Layers(LayeredImage image)
 {
     _image = image;
 }