예제 #1
0
        public void LoadFrames()
        {
            System.Drawing.Imaging.FrameDimension frameDimension = new System.Drawing.Imaging.FrameDimension(Image.FrameDimensionsList[0]);
            Data.FramesCount = Image.GetFrameCount(frameDimension);

            for (int i = 0; i < Data.FramesCount; i++)
            {
                if (Data.CancelLoading)
                {
                    return;
                }

                Image.SelectActiveFrame(frameDimension, i);
                Quantizer = new ImageManipulation.OctreeQuantizer(255, 8);

                System.Drawing.Bitmap quantized = Quantizer.Quantize(Image);
                MemoryStream          stream    = new MemoryStream();
                quantized.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
                Data.Frames.Add(new Texture(stream));

                stream.Dispose();

                if (Data.CancelLoading)
                {
                    return;
                }

                Data.Frames[i].Smooth = Data.Smooth;
                Data.Frames[i].Mipmap = Data.Mipmap;
            }
            Data.FullyLoaded = true;
        }
예제 #2
0
        public GifParser(string sPath)
        {
            Image MasterImage = Image.FromFile(sPath);

            ImageManipulation.OctreeQuantizer quantizer = null;
            FrameDimension oDimension = new FrameDimension(MasterImage.FrameDimensionsList[0]);
            int            FrameCount = MasterImage.GetFrameCount(oDimension);

            im = new Bitmap[FrameCount];
            for (int i = 0; i < FrameCount; i++)
            {
                MasterImage.SelectActiveFrame(oDimension, i);
                quantizer = new ImageManipulation.OctreeQuantizer(255, 8);
                im[i]     = quantizer.Quantize(MasterImage);
            }
            MasterImage.Dispose();
        }
예제 #3
0
        public void LoadFrames()
        {
            System.Drawing.Imaging.FrameDimension frameDimension = new System.Drawing.Imaging.FrameDimension(Image.FrameDimensionsList[0]);
            Data.FramesCount = Image.GetFrameCount(frameDimension);

            for (int i = 0; i < Image.GetFrameCount(frameDimension); i++)
            {
                Image.SelectActiveFrame(frameDimension, i);
                Quantizer = new ImageManipulation.OctreeQuantizer(255, 8);

                System.Drawing.Bitmap quantized = Quantizer.Quantize(Image);
                MemoryStream stream = new MemoryStream();
                quantized.Save(stream, System.Drawing.Imaging.ImageFormat.Gif);
                Data.Frames.Add(new Texture(stream));

                stream.Dispose();

                Data.Frames[i].Smooth = true;
            }
        }
예제 #4
0
        public void ProcessRequest(System.Web.HttpContext context)
        {
            DataRow row = Config.GetConfigTable("counter.config", "counter").Rows[0];

            if (context.Request.QueryString["id"] != null)
            {
                row = Config.GetConfigTable("counter.config", "counter").Select("id=" + context.Request.QueryString["id"])[0];
            }

            if (context.Request.QueryString["nocount"] == null)
            {
                #region подсчитать
                Hashtable h = Util.GetClientInfo(context.Request);
                if (context.Request.QueryString["path"] != null)
                {
                    h["path"] = Util.CreateHtmlLink(context.Request.QueryString["path"]);
                }
                else
                {
                    h["path"] = h["referer"];
                }
                if (context.Request.QueryString["ref"] != null)
                {
                    h["referer"] = Util.CreateHtmlLink(context.Request.QueryString["ref"]);
                }
                else
                {
                    h["referer"] = null;
                }
                Log.Request(h);
                #endregion
            }
            string cacheKey   = "CounterImageBytes";
            byte[] imageBytes = null;
            if (context.Cache[cacheKey] == null)
            {
                string file = context.Request.MapPath(row["img"].ToString());

                //существует ли файл
                if (!File.Exists(file))
                {
                    return;
                }
                Font  font  = new Font(row["font"].ToString(), Convert.ToInt32(row["size"]));
                Brush brush = Brushes.Black;
                if (row.Table.Columns.Contains("rgb"))
                {
                    string[] rgb = row["rgb"].ToString().Split(',');
                    System.Globalization.NumberStyles nstyle = System.Globalization.NumberStyles.AllowHexSpecifier;
                    brush = new SolidBrush(Color.FromArgb(int.Parse(rgb[0].Trim(), nstyle), int.Parse(rgb[1].Trim(), nstyle), int.Parse(rgb[2].Trim(), nstyle)));
                }
                int padding = Convert.ToInt32(row["padding"]);

                int all   = Log.GetCount("Unique") + Convert.ToInt32(row["start"]);
                int today = Log.GetCountToday("Unique");

                //определяем формат
                ImageFormat imageFormat = ImageFormat.Png;
                string      conttype    = "image/png";
                string      ext         = GetExtention(file).ToLower();
                switch (ext)
                {
                case "gif":
                    imageFormat = ImageFormat.Gif;
                    conttype    = "image/gif";
                    break;

                case "jpg":
                case "jpeg":
                    imageFormat = ImageFormat.Jpeg;
                    conttype    = "image/jpeg";
                    break;

                case "bmp":
                    imageFormat = ImageFormat.Bmp;
                    conttype    = "image/bmp";
                    break;
                }
                context.Response.ContentType = conttype;

                Bitmap       bitmap = null;
                Bitmap       image  = null;
                MemoryStream s      = null;
                try
                {
                    bitmap = new Bitmap(file);
                    image  = (Bitmap)bitmap.GetThumbnailImage(bitmap.Size.Width, bitmap.Size.Height, null, IntPtr.Zero);
                    Graphics g = Graphics.FromImage(image);

                    float x1 = padding;
                    float y1 = image.Size.Height - padding - g.MeasureString(all.ToString(), font).Height;
                    g.DrawString(all.ToString(), font, brush, x1, y1);

                    float x2 = image.Size.Width - padding - g.MeasureString("+" + today.ToString(), font).Width;
                    float y2 = y1;
                    g.DrawString("+" + today.ToString(), font, brush, x2, y2);
                    g.Dispose();

                    //коррекция GIF////////////////
                    if (imageFormat == ImageFormat.Gif)
                    {
                        ImageManipulation.OctreeQuantizer quantizer = new ImageManipulation.OctreeQuantizer(255, 8);
                        image = quantizer.Quantize(image);
                    }
                    /////////////////////////////


                    s = new MemoryStream();
                    image.Save(s, imageFormat);


                    imageBytes = s.ToArray();

                    //коррекция PNG////////////////
                    if (imageFormat == ImageFormat.Png)
                    {
                        imageBytes = PNGFix.RemoveImageGamma(imageBytes);
                    }
                    /////////////////////////////
                }
                finally
                {
                    if (bitmap != null)
                    {
                        bitmap.Dispose();
                    }
                    if (image != null)
                    {
                        image.Dispose();
                    }
                    if (s != null)
                    {
                        s.Close();
                    }
                }
                context.Cache.Insert(cacheKey, imageBytes, new CacheDependency(file), DateTime.Now.AddSeconds(30), TimeSpan.Zero);
            }
            else
            {
                imageBytes = (byte[])context.Cache[cacheKey];
            }
            //кэширование
            context.Response.Cache.SetExpires(DateTime.Now);
            context.Response.Cache.SetCacheability(System.Web.HttpCacheability.NoCache);
            context.Response.BinaryWrite(imageBytes);
        }
예제 #5
0
        public static byte[] GenerateImage(
            System.Web.HttpContext context,
            string profile,
            string file,
            ImagerConfig config,
            ImageFormat imageFormat)
        {
            if (config.IsEmpty)
            {
                config = GetImagerConfig(context, profile);
                if (config.IsEmpty)
                {
                    return(null);
                }
            }

            //существует ли файл
            bool   fileExists = false;
            Bitmap fromUrl    = null;

            if (file.StartsWith("http://"))
            {
                try
                {
                    HttpWebRequest  req           = (HttpWebRequest)WebRequest.Create(file);
                    HttpWebResponse res           = (HttpWebResponse)req.GetResponse();
                    Stream          receiveStream = res.GetResponseStream();
                    fromUrl = new Bitmap(receiveStream);
                    receiveStream.Close();
                    res.Close();
                    fileExists = true;
                }
                catch
                { }
            }
            else if (File.Exists(file))
            {
                fileExists = true;
            }



            if (!fileExists)
            {
                if (!Util.IsBlank(config.NotFoundImage))
                {
                    file = context.Request.MapPath(config.NotFoundImage);
                }
                else
                {
                    return(null);
                }
            }

            if (imageFormat == null)
            {
                imageFormat = GetImageFormat(file, config);
            }


            int w1 = -1;
            int h1 = -1;

            if (!Util.IsBlank(config.Width))
            {
                w1 = int.Parse(config.Width);
            }
            if (!Util.IsBlank(config.Height))
            {
                h1 = int.Parse(config.Height);
            }
            string fg = string.Empty;
            string bg = string.Empty;
            string y  = string.Empty;
            string x  = string.Empty;
            string fy = string.Empty;
            string fx = string.Empty;

            if (config.BackgroundImage != null)
            {
                bg = context.Request.MapPath(config.BackgroundImage);
                if (!Util.IsBlank(config.AlignX))
                {
                    x = config.AlignX;
                }
                if (!Util.IsBlank(config.AlignY))
                {
                    y = config.AlignY;
                }
            }
            if (!Util.IsBlank(config.FrontImage))
            {
                fg = context.Request.MapPath(config.FrontImage);
                if (!Util.IsBlank(config.FrontAlignX))
                {
                    fx = config.FrontAlignX;
                }
                if (!Util.IsBlank(config.FrontAlignY))
                {
                    fy = config.FrontAlignY;
                }
            }

            CompositingQuality cq = CompositingQuality.Default;

            if (!Util.IsBlank(config.CompositingQuality))
            {
                cq = (CompositingQuality)Enum.Parse(typeof(CompositingQuality), config.CompositingQuality, true);
            }
            SmoothingMode sm = SmoothingMode.Default;

            if (!Util.IsBlank(config.SmoothingMode))
            {
                sm = (SmoothingMode)Enum.Parse(typeof(SmoothingMode), config.SmoothingMode, true);
            }

            InterpolationMode im = InterpolationMode.Default;

            if (!Util.IsBlank(config.InterpolationMode))
            {
                im = (InterpolationMode)Enum.Parse(typeof(InterpolationMode), config.InterpolationMode, true);
            }


            Bitmap bitmap = null;
            Bitmap image  = null;

            try
            {
                if (fromUrl != null)
                {
                    bitmap = fromUrl;
                }
                else if (File.Exists(file))
                {
                    bitmap = new Bitmap(file);
                }
                else
                {
                    bitmap = new Bitmap(1, 1);
                }
                //определяем необходимый размер

                if (w1 == -1)
                {
                    w1 = bitmap.Width;
                }
                if (h1 == -1)
                {
                    h1 = bitmap.Height;
                }

                SizeF maxSize = new Size(w1, h1);
                SizeF newSize = bitmap.Size;
                int   cx      = 0;
                int   cy      = 0;

                //определяем метод уменьшения
                if (Util.IsBlank(config.Crop))//уменьшение пропорциональное
                {
                    if (newSize.Height > maxSize.Height)
                    {
                        newSize.Width  = newSize.Width * (maxSize.Height / newSize.Height);
                        newSize.Height = maxSize.Height;
                    }
                    if (newSize.Width > maxSize.Width)
                    {
                        newSize.Height = newSize.Height * (maxSize.Width / newSize.Width);
                        newSize.Width  = maxSize.Width;
                    }
                    image = new Bitmap(newSize.ToSize().Width, newSize.ToSize().Height);
                }
                else//вырезаем картинку нужного размера
                {
                    if (maxSize.Width > newSize.Width)
                    {
                        maxSize.Width = newSize.Width;
                    }
                    if (maxSize.Height > newSize.Height)
                    {
                        maxSize.Height = newSize.Height;
                    }
                    string[] crop = config.Crop.Split(',');

                    image = new Bitmap(maxSize.ToSize().Width, maxSize.ToSize().Height);
                    if (crop[0] == "1")//не масштабируем оригинал
                    {
                        if (crop.Length > 1)
                        {
                            switch (crop[1])
                            {
                            case "l":     //left
                                break;

                            case "r":     //right
                                cx = bitmap.Width - image.Width;
                                break;

                            case "c":     //center
                                cx = (bitmap.Width - image.Width) / 2;
                                break;

                            default:
                                cx = int.Parse(crop[1]);
                                break;
                            }
                            if (crop.Length > 2)
                            {
                                switch (crop[2])
                                {
                                case "t":     //top
                                    break;

                                case "b":     //bottom
                                    cy = bitmap.Height - image.Height;
                                    break;

                                case "m":     //middle
                                    cy = (bitmap.Height - image.Height) / 2;
                                    break;

                                default:
                                    cy = int.Parse(crop[2]);
                                    break;
                                }
                            }
                            else
                            {
                                cy = (bitmap.Height - image.Height) / 2;
                            }
                        }
                        else
                        {
                            cx = (bitmap.Width - image.Width) / 2;
                            cy = (bitmap.Height - image.Height) / 2;
                        }
                    }
                    else//уменьшаем насколько возможно и выбрасываем ненужную часть
                    {
                        bool   isW = true;
                        double s1  = maxSize.Height / maxSize.Width;
                        double s2  = newSize.Height / newSize.Width;
                        if (s1 > s2)
                        {
                            isW = false;
                        }
                        if (isW)
                        {
                            newSize.Height = newSize.Height * (maxSize.Width / newSize.Width);
                            newSize.Width  = maxSize.Width;
                        }
                        else
                        {
                            newSize.Width  = newSize.Width * (maxSize.Height / newSize.Height);
                            newSize.Height = maxSize.Height;
                        }
                        if (newSize.Width > maxSize.Width)
                        {
                            if (crop.Length > 1)
                            {
                                switch (crop[1])
                                {
                                case "l":     //left
                                    break;

                                case "r":     //right
                                    cx = (int)Math.Round(newSize.Width - maxSize.Width);
                                    break;

                                case "c":     //center
                                    cx = (int)Math.Round((newSize.Width - maxSize.Width) / 2);
                                    break;

                                default:
                                    cx = int.Parse(crop[1]);
                                    break;
                                }
                            }
                            else
                            {
                                cx = (int)Math.Round((newSize.Width - maxSize.Width) / 2);
                            }
                        }
                        else
                        {
                            if (crop.Length > 2)
                            {
                                switch (crop[2])
                                {
                                case "t":     //top
                                    break;

                                case "b":     //bottom
                                    cy = (int)Math.Round(newSize.Height - maxSize.Height);
                                    break;

                                case "m":     //middle
                                    cy = (int)Math.Round((newSize.Height - maxSize.Height) / 2);
                                    break;

                                default:
                                    cy = int.Parse(crop[2]);
                                    break;
                                }
                            }
                            else
                            {
                                cy = (int)Math.Round((newSize.Height - maxSize.Height) / 2);
                            }
                        }
                    }
                }
                Graphics gr = Graphics.FromImage(image);
                gr.SmoothingMode      = sm;
                gr.CompositingQuality = cq;
                gr.InterpolationMode  = im;
                if (!Util.IsBlank(config.BackgroundColor))
                {
                    string rgb = config.BackgroundColor;
                    if (rgb.Length == 6)
                    {
                        System.Globalization.NumberStyles nstyle = System.Globalization.NumberStyles.AllowHexSpecifier;
                        Color c = Color.FromArgb(int.Parse(rgb.Substring(0, 2), nstyle), int.Parse(rgb.Substring(2, 2), nstyle), int.Parse(rgb.Substring(4, 2), nstyle));
                        gr.Clear(c);
                    }
                }
                gr.DrawImage(bitmap, -1 * cx, -1 * cy, newSize.ToSize().Width, newSize.ToSize().Height);
                gr.Dispose();

                //Фоновое изображение
                if (File.Exists(bg))
                {
                    Bitmap   bg1 = new Bitmap(bg);
                    Bitmap   bgB = new Bitmap(bg1.Width, bg1.Height);
                    Graphics g   = Graphics.FromImage(bgB);
                    g.SmoothingMode      = sm;
                    g.CompositingQuality = cq;
                    g.InterpolationMode  = im;
                    g.DrawImage(bg1, 0, 0, bg1.Width, bg1.Height);
                    bg1.Dispose();
                    float y1 = 0;
                    float x1 = 0;
                    if (x != string.Empty)
                    {
                        switch (x)
                        {
                        case "l":     //left
                            break;

                        case "r":     //right
                            x1 = bgB.Width - image.Width;
                            break;

                        case "c":     //center
                            x1 = (bgB.Width - image.Width) / 2;
                            break;

                        default:
                            try
                            {
                                x1 = int.Parse(x);
                            }
                            catch { }
                            break;
                        }
                    }
                    if (y != string.Empty)
                    {
                        switch (y)
                        {
                        case "t":     //top
                            break;

                        case "b":     //bottom
                            y1 = bgB.Height - image.Height;
                            break;

                        case "m":     //middle
                            y1 = (bgB.Height - image.Height) / 2;
                            break;

                        default:
                            try
                            {
                                y1 = int.Parse(y);
                            }
                            catch { }
                            break;
                        }
                    }
                    g.DrawImage(image, x1, y1, image.Width, image.Height);
                    g.Dispose();
                    image.Dispose();
                    image = bgB;
                }
                //Рамка
                if (File.Exists(fg))
                {
                    Bitmap fg1 = new Bitmap(fg);
                    float  y1  = 0;
                    float  x1  = 0;
                    if (fx != string.Empty)
                    {
                        switch (fx)
                        {
                        case "l":     //left
                            break;

                        case "r":     //right
                            x1 = image.Width - fg1.Width;
                            break;

                        case "c":     //center
                            x1 = (image.Width - fg1.Width) / 2;
                            break;

                        default:
                            try
                            {
                                x1 = int.Parse(fx);
                            }
                            catch { }
                            break;
                        }
                    }
                    if (fy != string.Empty)
                    {
                        switch (fy)
                        {
                        case "t":     //top
                            break;

                        case "b":     //bottom
                            y1 = image.Height - fg1.Height;
                            break;

                        case "m":     //middle
                            y1 = (image.Height - fg1.Height) / 2;
                            break;

                        default:
                            try
                            {
                                y1 = int.Parse(fy);
                            }
                            catch { }
                            break;
                        }
                    }

                    Graphics g = Graphics.FromImage(image);
                    g.SmoothingMode      = sm;
                    g.CompositingQuality = cq;
                    g.InterpolationMode  = im;
                    g.DrawImage(fg1, x1, y1, fg1.Width, fg1.Height);
                    g.Dispose();
                    fg1.Dispose();
                }

                //коррекция GIF////////////////
                if (imageFormat == ImageFormat.Gif)
                {
                    ImageManipulation.OctreeQuantizer quantizer = new ImageManipulation.OctreeQuantizer(255, 8);
                    image = quantizer.Quantize(image);
                }
                /////////////////////////////

                MemoryStream s = new MemoryStream();

                //коррекция JPG////////////////
                if (imageFormat == ImageFormat.Jpeg)
                {
                    int quality = 90;
                    if (!Util.IsBlank(config.JpegQuality))
                    {
                        quality = int.Parse(config.JpegQuality);
                    }
                    Sota.Web.SimpleSite.JpegQuality.Save(image, s, quality);
                }
                /////////////////////////////
                else
                {
                    image.Save(s, imageFormat);
                }
                byte[] imageBytes = s.ToArray();
                s.Close();

                //коррекция PNG////////////////
                if (imageFormat == ImageFormat.Png)
                {
                    imageBytes = Sota.Web.SimpleSite.PNGFix.RemoveImageGamma(imageBytes);
                }
                /////////////////////////////

                return(imageBytes);
            }
            finally
            {
                if (bitmap != null)
                {
                    bitmap.Dispose();
                }
                if (image != null)
                {
                    image.Dispose();
                }
            }
        }