Exemplo n.º 1
0
    public static void CreateThumbnail(string imageFilePath, string thumbnailFilePath, System.Drawing.Size size, bool removeTransparency)
    {
        if (string.IsNullOrEmpty(imageFilePath))
        {
            throw new ArgumentNullException("imageFilePath");
        }
        if (string.IsNullOrEmpty(thumbnailFilePath))
        {
            throw new ArgumentNullException("thumbnailFilePath");
        }
        if (size.IsEmpty)
        {
            throw new ArgumentException("Thumbnail size can not be empty", "size");
        }

        if (File.Exists(imageFilePath))
        {
            var pipeline = new Pipeline();

            var reader = ImageReader.Create(imageFilePath);

            pipeline.Add(reader);

            var resizer = new Resize(size.Width, size.Height)
            {
                InterpolationMode = ResizeInterpolationMode.High, ResizeMode = ResizeMode.Fit
            };

            pipeline.Add(resizer);

            if (removeTransparency && reader.PixelFormat.HasAlpha)
            {
                var cc = new ColorConverter(PixelFormat.DiscardAlpha(reader.PixelFormat));
                cc.BackgroundColor = cc.ConvertColor(new RgbColor(255, 255, 255));

                pipeline.Add(cc);
            }

            var writer = ImageWriter.Create(thumbnailFilePath);

            if (writer is GifWriter)
            {
                ColorPalette palette = null;

                using (var bitmap = reader.Frames[0].GetBitmap())
                    palette = ColorPalette.Create(bitmap);

                pipeline.Add(new ColorConverter(PixelFormat.Format8bppIndexed)
                {
                    Palette = palette
                });
            }

            pipeline.Add(writer);

            pipeline.Run();

            pipeline.DisposeAllElements();
        }
    }
    /// <summary>
    /// Converts to indexed pixel format (optimized for Web)
    /// </summary>
    private static void ConvertToIndexed()
    {
        using (var bitmap = new Bitmap("../../../../_Input/Copenhagen_RGB.jpg"))
        {
            bitmap.ColorManagement.Dithering = DitheringType.FloydSteinberg;
            bitmap.ColorManagement.Palette   = ColorPalette.Create(bitmap, 64);
            bitmap.ColorManagement.Convert(PixelFormat.Format8bppIndexed);

            bitmap.Save("../../../../_Output/PF_ConvertToIndexed.png");
        }
    }
Exemplo n.º 3
0
 public void DetectColorPalettes()
 {
     ColorPalettes = new HashSet <ColorPalette>();
     foreach (var file in Directory.GetFiles("Palettes", "*.png"))
     {
         var filename = Path.GetFullPath(file);
         try
         {
             var pixelColors = BitmapUtils.LoadGlyphColors(filename);
             var name        = Path.GetFileNameWithoutExtension(file);
             var glyphFont   = ColorPalette.Create(name, pixelColors);
             ColorPalettes.Add(glyphFont);
         }
         catch (Exception e)
         {
             ColorPalettes.Add(ColorPalette.CreateInvalid(filename, "Could not load palette from file: " + e.Message));
         }
     }
     MessageBus.Publish(new ColorPaletteListLoadedEvent(ColorPalettes.ToArray()));
 }