示例#1
0
        public void Write(System.Drawing.Image i, System.IO.Stream s)
        {
            if (!(i is Bitmap))
            {
                throw new ArgumentException("FreeImageEncoder only works with bitmaps");
            }
            FIBITMAP bit = FreeImage.CreateFromBitmap(i as Bitmap);

            if (bit.IsNull)
            {
                throw new ImageProcessingException("FreeImageEncoder failed to convert System.Drawing.Bitmap to FIBITMAP");
            }
            if (Format == FREE_IMAGE_FORMAT.FIF_GIF || (Format == FREE_IMAGE_FORMAT.FIF_PNG && colors != -1))
            {
                FreeImage.SetTransparent(bit, true);
                FIBITMAP old = bit;
                //TODO - ColorQuantizeEx returns null no matter what we do.. Is it because the image is 32-bit?
                bit = FreeImage.ColorQuantizeEx(bit, FREE_IMAGE_QUANTIZE.FIQ_NNQUANT, 256, 1, RGBQUAD.ToRGBQUAD(new Color[] { Color.Transparent }));
                if (bit.IsNull)
                {
                    bit = old;
                }
                else if (bit != old)
                {
                    FreeImage.UnloadEx(ref old);
                }
            }
            FreeImage.SaveToStream(ref bit, s, Format, EncodingOptions, true);
        }
示例#2
0
        /// <summary>
        /// Stick many images to a single by vertical
        /// </summary>
        /// <param name="images"></param>
        /// <returns></returns>
        public FIBITMAP ImplodeImages(Bitmap[] images)
        {
            FIBITMAP dib;

            // size of single bitmap
            var width  = images[0].Width;
            var height = images[0].Height;
            // size of canvas
            var cwidth  = width;
            var cheight = height * images.Length;

            using (var bitmap = new Bitmap(cwidth, cheight))
            {
                using (var canvas = Graphics.FromImage(bitmap))
                {
                    // draw bitmaps on canvas
                    canvas.InterpolationMode = InterpolationMode.HighQualityBicubic;
                    for (int i = 0; i < images.Length; i++)
                    {
                        canvas.DrawImage(images[i], new Rectangle(0, height * i, width, height), new Rectangle(0, 0, width, height), GraphicsUnit.Pixel);
                    }
                    canvas.Save();

                    // convert canvas to freeimage
                    dib = FreeImage.CreateFromBitmap(bitmap);
                }
            }

            return(dib);
        }
示例#3
0
        private void SaveToSer_Click(object sender, EventArgs e)
        {
            // Create a new dialog
            SaveFileDialog sfd = new SaveFileDialog();

            FIBITMAP dib = 0;

            try
            {
                // Check if the picture box contains a bitmap that can be saved.
                if (pictureBox.Image == null)
                {
                    MessageBox.Show("No bitmap loaded.", "Error");
                    return;
                }

                // Convert the picture-boxes bitmap into a FreeImage bitmap.
                dib = FreeImage.CreateFromBitmap((Bitmap)pictureBox.Image);
                if (dib.IsNull)
                {
                    MessageBox.Show("Unable to convert bitmap to FIBITMAP.", "Error");
                    return;
                }

                // Apply settings
                sfd.Filter           = "Serialized bitmap (*.ser)|*.ser";
                sfd.FileName         = "Bitmap.ser";
                sfd.OverwritePrompt  = true;
                sfd.RestoreDirectory = true;

                // Get filename
                if (sfd.ShowDialog() == DialogResult.OK)
                {
                    // Save bitmap in the new format
                    if (FreeImage.SaveEx(dib, sfd.FileName, serialPlugin.Format))
                    {
                        MessageBox.Show("Bitmap saved successfully.", "Success");
                    }
                    else
                    {
                        MessageBox.Show("Saving bitmap failed.", "Failure");
                    }
                }
                else
                {
                    MessageBox.Show("Action aborted.");
                }
            }
            finally
            {
                // Unload bitmap
                FreeImage.UnloadEx(ref dib);

                // Unload dialog
                sfd.Dispose();
            }
        }
        public static Bitmap ConvertTo4bpp(System.Drawing.Bitmap img)
        {
            Bitmap   res;
            FIBITMAP dib = FreeImage.CreateFromBitmap(img);

            dib = FreeImage.ConvertColorDepth(dib, FREE_IMAGE_COLOR_DEPTH.FICD_04_BPP, true);
            res = FreeImage.GetBitmap(dib);
            FreeImage.UnloadEx(ref dib);
            return(res);
        }
        }//IsPointIn()

        /// <summary>
        /// rotate image by dAngleDegree_
        /// </summary>
        /// <param name="dAngle_"> rotatio angle in degree </param>
        /// <returns></returns>
        public bool RotateImage(double dAngleDegree_)
        {
            if (m_image != null)
            {
                FIBITMAP newFiBitmap = FreeImage.CreateFromBitmap(( Bitmap )m_image);
                newFiBitmap = FreeImage.Rotate(newFiBitmap, dAngleDegree_);   // rotate by Ccw
                m_image     = FreeImage.GetBitmap(newFiBitmap);

                return(true);
            }//if

            return(false);
        }//RotateImageCcw()
示例#6
0
        //private string getSuffix(ExportFileType type) {

        //}

        private DibMd loadFile(string srcPath, int icoSize)
        {
            string srcSuffix = Path.GetExtension(srcPath).ToLower();

            FIBITMAP dib;

            try {
                switch (srcSuffix)
                {
                case ".ico": {
                    dib = FreeImage.Load(FREE_IMAGE_FORMAT.FIF_ICO, srcPath, FREE_IMAGE_LOAD_FLAGS.DEFAULT);
                    break;
                }

                case ".bmp": {
                    dib = FreeImage.Load(FREE_IMAGE_FORMAT.FIF_BMP, srcPath, FREE_IMAGE_LOAD_FLAGS.DEFAULT);
                    break;
                }

                case ".jpg": {
                    dib = FreeImage.Load(FREE_IMAGE_FORMAT.FIF_JPEG, srcPath, FREE_IMAGE_LOAD_FLAGS.DEFAULT);
                    break;
                }

                case ".png": {
                    dib = FreeImage.Load(FREE_IMAGE_FORMAT.FIF_PNG, srcPath, FREE_IMAGE_LOAD_FLAGS.DEFAULT);
                    break;
                }

                default: {
                    Bitmap img = FileIcon.getIcon(srcPath, icoSize);
                    if (img == null)
                    {
                        return(null);
                    }
                    dib = FreeImage.CreateFromBitmap(img);
                    break;
                    //return;
                }
                }
                return(new DibMd(dib));
            } catch (Exception) { }

            return(null);
        }
示例#7
0
        /// <summary>
        /// Convert bitmap -> tga
        /// </summary>
        /// <param name="image">image to convert</param>
        /// <returns></returns>
        public FIBITMAP Bitmap2Tga(Bitmap image)
        {
            FIBITMAP dib;

            using (var bitmap = new Bitmap(image.Width, image.Height))
            {
                using (var canvas = Graphics.FromImage(bitmap))
                {
                    // draw bitmap on canvas
                    canvas.InterpolationMode = InterpolationMode.HighQualityBicubic;
                    canvas.DrawImage(image, 0, 0, image.Width, image.Height);
                    canvas.Save();

                    // convert canvas to freeimage
                    dib = FreeImage.CreateFromBitmap(bitmap);
                }
            }
            return(dib);
        }
        static void Main(string[] args)
        {
            FIBITMAP bitmap;
            bool     success;

            SetOutputMessage(OutputMessage);

            Console.Write("Test 1... ");
            bitmap  = FreeImage.CreateFromBitmap(new Bitmap(320, 240, PixelFormat.Format24bppRgb));
            success = FreeImage.Save(FREE_IMAGE_FORMAT.FIF_JPEG, bitmap, "output.jpg", FREE_IMAGE_SAVE_FLAGS.JPEG_QUALITYNORMAL);
            FreeImage.Unload(bitmap);
            Console.WriteLine("Success");

            Console.Write("Test 2... ");
            bitmap  = FreeImage.CreateFromBitmap(new Bitmap(320, 240));
            success = FreeImage.Save(FREE_IMAGE_FORMAT.FIF_JPEG, bitmap, "output.jpg", FREE_IMAGE_SAVE_FLAGS.JPEG_QUALITYNORMAL);
            FreeImage.Unload(bitmap);
            Console.WriteLine("Success");
        }
示例#9
0
        private void bExample03_Click(object sender, EventArgs e)
        {
            // Load bitmap
            Bitmap bitmap = (Bitmap)Bitmap.FromFile("Sample.jpg");

            // Convert the .NET bitmap into a FreeImage-Bitmap
            FIBITMAP dib = FreeImage.CreateFromBitmap(bitmap);

            // Unload bitmap
            bitmap.Dispose();

            // Rescale the bitmap
            FIBITMAP temp = FreeImage.Rescale(dib, 300, 300, FREE_IMAGE_FILTER.FILTER_BICUBIC);

            // Unload bitmap
            FreeImage.UnloadEx(ref dib);

            Random rand = new Random();

            // Rotate the bitmap
            dib = FreeImage.RotateClassic(temp, rand.NextDouble() * 360d);

            // Unload bitmap
            FreeImage.UnloadEx(ref temp);

            // Convert the FreeImage-Bitmap into a .NET bitmap
            bitmap = FreeImage.GetBitmap(dib);

            // Unload bitmap
            FreeImage.UnloadEx(ref dib);

            // Unload bitmap
            if (picBox.Image != null)
            {
                picBox.Image.Dispose();
            }

            // Assign new bitmap
            picBox.Image = bitmap;
        }
示例#10
0
        private void convert(string srcPath, string dstPath, int icoSize, int bpp)
        {
            string srcSuffix = Path.GetExtension(srcPath).ToLower();
            string dstSuffix = Path.GetExtension(dstPath).ToLower();

            //bool isDir = Directory.Exists(srcPath);

            // save
            try {
                FIBITMAP dib;
                switch (srcSuffix)
                {
                case ".ico": {
                    dib = FreeImage.Load(FREE_IMAGE_FORMAT.FIF_ICO, srcPath, FREE_IMAGE_LOAD_FLAGS.DEFAULT);
                    break;
                }

                case ".bmp": {
                    dib = FreeImage.Load(FREE_IMAGE_FORMAT.FIF_BMP, srcPath, FREE_IMAGE_LOAD_FLAGS.DEFAULT);
                    break;
                }

                case ".jpg": {
                    dib = FreeImage.Load(FREE_IMAGE_FORMAT.FIF_JPEG, srcPath, FREE_IMAGE_LOAD_FLAGS.DEFAULT);
                    break;
                }

                case ".png": {
                    dib = FreeImage.Load(FREE_IMAGE_FORMAT.FIF_PNG, srcPath, FREE_IMAGE_LOAD_FLAGS.DEFAULT);
                    break;
                }

                default: {
                    Bitmap img = FileIcon.getIcon(srcPath, icoSize);
                    if (img == null)
                    {
                        return;
                    }
                    dib = FreeImage.CreateFromBitmap(img);
                    break;
                    //return;
                }
                }
                //FIBITMAP dib = FreeImage.Load(FREE_IMAGE_FORMAT.FIF_PNG, srcPath, FREE_IMAGE_LOAD_FLAGS.DEFAULT);
                uint width  = FreeImage.GetWidth(dib);
                uint height = FreeImage.GetHeight(dib);
                //FIBITMAP dibTmp = FreeImage.Rescale(dib, icoSize, icoSize, FREE_IMAGE_FILTER.FILTER_BICUBIC);

                FIBITMAP dibOut = formatImage(dib, icoSize, bpp);

                FREE_IMAGE_FORMAT     format = FREE_IMAGE_FORMAT.FIF_ICO;
                FREE_IMAGE_SAVE_FLAGS flag   = FREE_IMAGE_SAVE_FLAGS.BMP_SAVE_RLE;
                switch (dstSuffix)
                {
                case ".png": format = FREE_IMAGE_FORMAT.FIF_PNG; flag = FREE_IMAGE_SAVE_FLAGS.PNG_Z_DEFAULT_COMPRESSION; break;

                case ".jpg": format = FREE_IMAGE_FORMAT.FIF_JPEG; flag = FREE_IMAGE_SAVE_FLAGS.JPEG_QUALITYGOOD; break;

                case ".bmp": format = FREE_IMAGE_FORMAT.FIF_BMP; flag = FREE_IMAGE_SAVE_FLAGS.BMP_SAVE_RLE; break;

                case ".ico":
                default: break;
                }
                FreeImage.Save(format, dibOut, dstPath, flag);
                //bool isOk = FreeImage.Save(FREE_IMAGE_FORMAT.FIF_PNG, dibOut, dstPath + ".png", FREE_IMAGE_SAVE_FLAGS.PNG_INTERLACED);
                FreeImage.Unload(dibOut);
                FreeImage.Unload(dib);
            } catch (Exception ex) { Debug.WriteLine(ex.ToString()); }
        }
示例#11
0
        protected override RequestedAction PreRenderImage(ImageState s)
        {
            //Skip this when we are doing simulations
            if (s.destGraphics == null)
            {
                return(RequestedAction.None);
            }

            string sf = s.settings["fi.scale"];

            if (string.IsNullOrEmpty(sf))
            {
                return(RequestedAction.None);
            }
            bool validAlg            = false;
            FREE_IMAGE_FILTER filter = ParseResizeAlgorithm(sf, FREE_IMAGE_FILTER.FILTER_CATMULLROM, out validAlg);

            if (!validAlg)
            {
                throw new ImageProcessingException("The specified resizing filter '" + sf + "' did not match bicubic, bilinear, box, bspline, catmullrom, or lanczos.");
            }

            //Set copy attributes
            s.copyAttibutes.SetWrapMode(WrapMode.TileFlipXY);


            //The minimum dimensions of the temporary bitmap.
            SizeF targetSize = PolygonMath.getParallelogramSize(s.layout["image"]);

            targetSize = new SizeF((float)Math.Ceiling(targetSize.Width), (float)Math.Ceiling(targetSize.Height));

            s.ApplyCropping();
            s.EnsurePreRenderBitmap();

            //The size of the temporary bitmap.
            //We want it larger than the size we'll use on the final copy, so we never upscale it
            //- but we also want it as small as possible so processing is fast.
            SizeF    tempSize   = PolygonMath.ScaleOutside(targetSize, s.copyRect.Size);
            int      tempWidth  = (int)Math.Ceiling(tempSize.Width);
            int      tempHeight = (int)Math.Ceiling(tempSize.Height);
            FIBITMAP src        = FIBITMAP.Zero;
            FIBITMAP midway     = FIBITMAP.Zero;

            try {
                var oldbit = s.preRenderBitmap ?? s.sourceBitmap;
                //Crop if needed, Convert, scale, then convert back.
                src = FreeImage.CreateFromBitmap(oldbit);

                midway = FreeImage.Rescale(src, tempWidth, tempHeight, filter);
                FreeImage.UnloadEx(ref src);
                //Clear the old pre-rendered image if needed
                if (s.preRenderBitmap != null)
                {
                    s.preRenderBitmap.Dispose();
                }
                //Reassign the pre-rendered image
                s.preRenderBitmap = FreeImage.GetBitmap(midway);
                s.copyRect        = new RectangleF(0, 0, s.preRenderBitmap.Width, s.preRenderBitmap.Height);
                FreeImage.UnloadEx(ref midway);
                s.preRenderBitmap.MakeTransparent();
            } finally {
                if (!src.IsNull)
                {
                    FreeImage.UnloadEx(ref src);
                }
                if (!midway.IsNull)
                {
                    FreeImage.UnloadEx(ref midway);
                }
            }


            return(RequestedAction.Cancel);
        }
示例#12
0
        public static Response GetPageImage(Guid id, int page, int width, int height, IResponseFormatter response)
        {
            // Restrict access to the FreeImage library to one thread at a time.
            lock (lockThis)
            {
                int  max_width  = 0;
                int  max_height = 0;
                bool thumbnail  = !(width == -1 && height == -1);
                bool processed  = false;

                string filename = string.Format("{0}-p{1}-w{2}-h{3}.jpg", id, page, width, height);

                if (thumbnail)
                {
                    MemoryStream cachestream = ImageCache.Instance.LoadFromCache(filename, true);
                    // Cached thumbnails are assumed to be in the correct format and adhere to the size/format restrictions of the ipad.
                    if (cachestream != null)
                    {
                        return(response.FromStream(cachestream, MimeTypes.GetMimeType(".jpg")));
                    }
                }
                else
                {
                    // Check if a processed (rescaled and/or progressive) image is cached.
                    string       processed_filename = string.Format("{0}-p{1}-processed.jpg", id, page);
                    MemoryStream cachestream        = ImageCache.Instance.LoadFromCache(processed_filename, false);
                    if (cachestream != null)
                    {
                        return(response.FromStream(cachestream, MimeTypes.GetMimeType(".jpg")));
                    }
                }

                MemoryStream stream = null;

                // Check if original image is in the cache.
                string org_filename = string.Format("{0}-p{1}.jpg", id, page);
                stream = ImageCache.Instance.LoadFromCache(org_filename, false);

                if (stream == null)
                {
                    // Image is not in the cache, get it via ComicRack.
                    var bytes = GetPageImageBytes(id, page);
                    if (bytes == null)
                    {
                        return(HttpStatusCode.NotFound);
                    }

                    stream = new MemoryStream(bytes);

                    // Always save the original page to the cache
                    ImageCache.Instance.SaveToCache(org_filename, stream, false);
                }

                stream.Seek(0, SeekOrigin.Begin);

            #if USE_GDI
                Bitmap bitmap        = new Bitmap(stream, false);
                int    bitmap_width  = (int)bitmap.Width;
                int    bitmap_height = (int)bitmap.Height;
            #elif USE_DIB
                FIBITMAP dib = FreeImage.LoadFromStream(stream);
                if (dib == null)
                {
                    Console.WriteLine("Loading bitmap failed. Aborting.");
                    // Check whether there was an error message.
                    return(HttpStatusCode.InternalServerError);
                }
                int bitmap_width  = (int)FreeImage.GetWidth(dib);
                int bitmap_height = (int)FreeImage.GetHeight(dib);
            #elif USE_FIB
                FreeImageBitmap fib = FreeImageBitmap.FromStream(stream, false);
                if (fib == null)
                {
                    Console.WriteLine("Loading bitmap failed. Aborting.");
                    // Check whether there was an error message.
                    return(HttpStatusCode.InternalServerError);
                }

                int bitmap_width  = (int)fib.Width;
                int bitmap_height = (int)fib.Height;
            #endif

                if (ImageCache.Instance.use_max_dimension)
                {
                    int mw, mh;

                    if (bitmap_width >= bitmap_height)
                    {
                        mw = ImageCache.Instance.max_dimension_long;
                        mh = ImageCache.Instance.max_dimension_short;
                    }
                    else
                    {
                        mw = ImageCache.Instance.max_dimension_short;
                        mh = ImageCache.Instance.max_dimension_long;
                    }

                    if (bitmap_width > mw || bitmap_height > mh)
                    {
                        double scaleW = (double)mw / (double)bitmap_width;
                        double scaleH = (double)mh / (double)bitmap_height;
                        double scale  = Math.Min(scaleW, scaleH);

                        max_width  = (int)Math.Floor(scale * bitmap_width);
                        max_height = (int)Math.Floor(scale * bitmap_height);
                    }
                    else
                    {
                        max_width  = bitmap_width;
                        max_height = bitmap_height;
                    }
                }
                else
                // Check if the image dimensions exceeds the maximum image dimensions
                if ((bitmap_width * bitmap_height) > ImageCache.Instance.maximum_imagesize)
                {
                    max_width  = (int)Math.Floor(Math.Sqrt((double)bitmap_width / (double)bitmap_height * (double)ImageCache.Instance.maximum_imagesize));
                    max_height = (int)Math.Floor((double)max_width * (double)bitmap_height / (double)bitmap_width);
                }
                else
                {
                    max_width  = bitmap_width;
                    max_height = bitmap_height;
                }

                // Calculate the dimensions of the returned image.
                int result_width  = width;
                int result_height = height;

                if (result_width == -1 && result_height == -1)
                {
                    result_width  = max_width;
                    result_height = max_height;
                }
                else
                {
                    if (result_width == -1)
                    {
                        result_height = Math.Min(max_height, result_height);
                        double ratio = (double)result_height / (double)max_height;
                        result_width = (int)Math.Floor(((double)max_width * ratio));
                    }
                    else
                    if (result_height == -1)
                    {
                        result_width = Math.Min(max_width, result_width);
                        double ratio = (double)result_width / (double)max_width;
                        result_height = (int)Math.Floor(((double)max_height * ratio));
                    }
                }

                // TODO: do this per requesting target device instead of using one global setting.

                // Resize ?
                if (result_width != bitmap_width || result_height != bitmap_height)
                {
                    processed = true;

              #if USE_DIB || USE_FIB
                    //FREE_IMAGE_FILTER resizer = FREE_IMAGE_FILTER.FILTER_BICUBIC;
                    FREE_IMAGE_FILTER resizer = FREE_IMAGE_FILTER.FILTER_LANCZOS3;

                #if USE_FIB
                    fib.Rescale(result_width, result_height, resizer);
                #else
                    FIBITMAP newdib = FreeImage.Rescale(dib, result_width, result_height, resizer);
                    if (!newdib.IsNull)
                    {
                        FreeImage.Unload(dib);
                        dib.SetNull();
                        dib = newdib;
                    }
                #endif
              #elif USE_GDI
                    Bitmap resizedBitmap = Resize(bitmap, result_width, result_height);
                    bitmap.Dispose();
                    bitmap        = resizedBitmap;
                    resizedBitmap = null;
              #endif
                }


                // Check if the image must be converted to progressive jpeg
                if (ImageCache.Instance.use_progressive_jpeg && (result_width * result_height) >= ImageCache.Instance.progressive_jpeg_size_threshold)
                {
                    processed = true;

                    // Convert image to progressive jpeg

                    // FreeImage source code reveals that lower 7 bits of the FREE_IMAGE_SAVE_FLAGS enum are used for low-level quality control.
                    FREE_IMAGE_SAVE_FLAGS quality = (FREE_IMAGE_SAVE_FLAGS)ImageCache.Instance.progressive_jpeg_quality;
                    FREE_IMAGE_SAVE_FLAGS flags   = FREE_IMAGE_SAVE_FLAGS.JPEG_SUBSAMPLING_444 | FREE_IMAGE_SAVE_FLAGS.JPEG_PROGRESSIVE | quality;

              #if USE_DIB || USE_FIB
                    stream.Dispose();
                    stream = new MemoryStream();

                #if USE_FIB
                    fib.Save(stream, FREE_IMAGE_FORMAT.FIF_JPEG, flags);
                    fib.Dispose();
                #else
                    FreeImage.SaveToStream(dib, stream, FREE_IMAGE_FORMAT.FIF_JPEG, flags);
                    FreeImage.Unload(dib);
                    dib.SetNull();
                #endif
              #else
                    FIBITMAP dib = FreeImage.CreateFromBitmap(bitmap);
                    bitmap.Dispose();
                    bitmap = null;
                    stream.Dispose();
                    stream = new MemoryStream();

                    FreeImage.SaveToStream(dib, stream, FREE_IMAGE_FORMAT.FIF_JPEG, flags);
                    FreeImage.Unload(dib);
                    dib.SetNull();
              #endif
                }
                else
                if (processed)
                {
                    // image was rescaled, make new stream with rescaled bitmap

              #if USE_DIB || USE_FIB
                    FREE_IMAGE_SAVE_FLAGS flags = FREE_IMAGE_SAVE_FLAGS.JPEG_SUBSAMPLING_444 | FREE_IMAGE_SAVE_FLAGS.JPEG_OPTIMIZE | FREE_IMAGE_SAVE_FLAGS.JPEG_QUALITYNORMAL;

                    stream.Dispose();
                    stream = new MemoryStream();

                #if USE_FIB
                    fib.Save(stream, FREE_IMAGE_FORMAT.FIF_JPEG, flags);
                    fib.Dispose();
                #else
                    FreeImage.SaveToStream(dib, stream, FREE_IMAGE_FORMAT.FIF_JPEG, flags);
                    FreeImage.Unload(dib);
                    dib.SetNull();
                #endif
              #else
                    stream = GetBytesFromImage(bitmap);
              #endif
                    // For now, images that were resized because they exceeded the maximum dimensions are not saved to the cache.
                }

            #if USE_DIB
                FreeImage.Unload(dib);
                dib.SetNull();
            #elif USE_FIB
                fib.Dispose();
            #elif USE_GDI
                if (bitmap != null)
                {
                    bitmap.Dispose();
                    bitmap = null;
                }
            #endif

                // Always save thumbnails to the cache
                if (thumbnail)
                {
                    ImageCache.Instance.SaveToCache(filename, stream, true);
                }
                else
                if (processed)
                {
                    // Store rescaled and/or progressive jpegs in the cache for now.
                    string processed_filename = string.Format("{0}-p{1}-processed.jpg", id, page);
                    ImageCache.Instance.SaveToCache(processed_filename, stream, false);
                }

                stream.Seek(0, SeekOrigin.Begin);
                return(response.FromStream(stream, MimeTypes.GetMimeType(".jpg")));
            }
        }
示例#13
0
        private void bSave_Click(object sender, EventArgs e)
        {
            // Check if there is a loaded bitmap
            if (picBox.Image == null)
            {
                MessageBox.Show("No image loaded.", "Error");
                return;
            }
            SaveFileDialog sfd     = null;
            FileStream     fStream = null;
            FIBITMAP       dib     = new FIBITMAP();

            try
            {
                sfd = new SaveFileDialog();
                sfd.CreatePrompt     = false;
                sfd.FileName         = "";
                sfd.Filter           = "TIF (*tif)|*.tif";
                sfd.OverwritePrompt  = true;
                sfd.RestoreDirectory = true;
                if (sfd.ShowDialog() == DialogResult.OK)
                {
                    // Convert the .NET bitmap into a FreeImage-Bitmap
                    dib = FreeImage.CreateFromBitmap((Bitmap)picBox.Image);
                    if (dib.IsNull)
                    {
                        throw new Exception();
                    }
                    // Create a filestream to write to
                    fStream = new FileStream(sfd.FileName, FileMode.Create);
                    if (!FreeImage.SaveToStream(
                            ref dib,
                            fStream,
                            FREE_IMAGE_FORMAT.FIF_TIFF,
                            FREE_IMAGE_SAVE_FLAGS.TIFF_LZW,
                            FREE_IMAGE_COLOR_DEPTH.FICD_AUTO,
                            false))
                    {
                        throw new Exception();
                    }
                    MessageBox.Show("Image saved successfully.", "Success");
                }
                else
                {
                    MessageBox.Show("Operation aborted.", "Aborted");
                }
            }
            catch
            {
                MessageBox.Show("Error saving image.", "Error");
            }
            finally
            {
                // Clean up
                if (sfd != null)
                {
                    sfd.Dispose();
                }
                if (fStream != null)
                {
                    fStream.Dispose();
                }
                FreeImage.UnloadEx(ref dib);
            }
        }
示例#14
0
文件: Program.cs 项目: radtek/AxTIF5
        void Run()
        {
            Bitmap pic = new Bitmap("Base.png");

            {
                String   fn  = "1bpp-CCITTFAX3";
                FIBITMAP p32 = FreeImage.CreateFromBitmap(Draws(fn, pic));
                FIBITMAP p1  = FreeImage.ConvertColorDepth(p32, FREE_IMAGE_COLOR_DEPTH.FICD_FORCE_GREYSCALE | FREE_IMAGE_COLOR_DEPTH.FICD_01_BPP);
                FreeImage.Save(FREE_IMAGE_FORMAT.FIF_TIFF, p1, fn + ".tif", FREE_IMAGE_SAVE_FLAGS.TIFF_CCITTFAX3);
                FreeImage.Unload(p1);
                FreeImage.Unload(p32);
            }
            {
                String   fn  = "1bpp-CCITTFAX4";
                FIBITMAP p32 = FreeImage.CreateFromBitmap(Draws(fn, pic));
                FIBITMAP p1  = FreeImage.ConvertColorDepth(p32, FREE_IMAGE_COLOR_DEPTH.FICD_FORCE_GREYSCALE | FREE_IMAGE_COLOR_DEPTH.FICD_01_BPP);
                FreeImage.Save(FREE_IMAGE_FORMAT.FIF_TIFF, p1, fn + ".tif", FREE_IMAGE_SAVE_FLAGS.TIFF_CCITTFAX4);
                FreeImage.Unload(p1);
                FreeImage.Unload(p32);
            }
            {
                String   fn  = "1bpp-DEFLATE";
                FIBITMAP p32 = FreeImage.CreateFromBitmap(Draws(fn, pic));
                FIBITMAP p1  = FreeImage.ConvertColorDepth(p32, FREE_IMAGE_COLOR_DEPTH.FICD_FORCE_GREYSCALE | FREE_IMAGE_COLOR_DEPTH.FICD_01_BPP);
                FreeImage.Save(FREE_IMAGE_FORMAT.FIF_TIFF, p1, fn + ".tif", FREE_IMAGE_SAVE_FLAGS.TIFF_DEFLATE);
                FreeImage.Unload(p1);
                FreeImage.Unload(p32);
            }
            {
                String   fn  = "1bpp-LZW";
                FIBITMAP p32 = FreeImage.CreateFromBitmap(Draws(fn, pic));
                FIBITMAP p1  = FreeImage.ConvertColorDepth(p32, FREE_IMAGE_COLOR_DEPTH.FICD_FORCE_GREYSCALE | FREE_IMAGE_COLOR_DEPTH.FICD_01_BPP);
                FreeImage.Save(FREE_IMAGE_FORMAT.FIF_TIFF, p1, fn + ".tif", FREE_IMAGE_SAVE_FLAGS.TIFF_LZW);
                FreeImage.Unload(p1);
                FreeImage.Unload(p32);
            }
            {
                String   fn  = "1bpp-PACKBITS";
                FIBITMAP p32 = FreeImage.CreateFromBitmap(Draws(fn, pic));
                FIBITMAP p1  = FreeImage.ConvertColorDepth(p32, FREE_IMAGE_COLOR_DEPTH.FICD_FORCE_GREYSCALE | FREE_IMAGE_COLOR_DEPTH.FICD_01_BPP);
                FreeImage.Save(FREE_IMAGE_FORMAT.FIF_TIFF, p1, fn + ".tif", FREE_IMAGE_SAVE_FLAGS.TIFF_PACKBITS);
                FreeImage.Unload(p1);
                FreeImage.Unload(p32);
            }
            {
                String   fn  = "1bpp-ADOBE_DEFLATE";
                FIBITMAP p32 = FreeImage.CreateFromBitmap(Draws(fn, pic));
                FIBITMAP p1  = FreeImage.ConvertColorDepth(p32, FREE_IMAGE_COLOR_DEPTH.FICD_FORCE_GREYSCALE | FREE_IMAGE_COLOR_DEPTH.FICD_01_BPP);
                FreeImage.Save(FREE_IMAGE_FORMAT.FIF_TIFF, p1, fn + ".tif", FREE_IMAGE_SAVE_FLAGS.TIFF_ADOBE_DEFLATE);
                FreeImage.Unload(p1);
                FreeImage.Unload(p32);
            }



            {
                String   fn  = "4bpp-DEFLATE";
                FIBITMAP p32 = FreeImage.CreateFromBitmap(Draws(fn, pic));
                FIBITMAP p1  = FreeImage.ConvertColorDepth(p32, FREE_IMAGE_COLOR_DEPTH.FICD_04_BPP);
                FreeImage.Save(FREE_IMAGE_FORMAT.FIF_TIFF, p1, fn + ".tif", FREE_IMAGE_SAVE_FLAGS.TIFF_DEFLATE);
                FreeImage.Unload(p1);
                FreeImage.Unload(p32);
            }
            {
                String   fn  = "4bpp-LZW";
                FIBITMAP p32 = FreeImage.CreateFromBitmap(Draws(fn, pic));
                FIBITMAP p1  = FreeImage.ConvertColorDepth(p32, FREE_IMAGE_COLOR_DEPTH.FICD_04_BPP);
                FreeImage.Save(FREE_IMAGE_FORMAT.FIF_TIFF, p1, fn + ".tif", FREE_IMAGE_SAVE_FLAGS.TIFF_LZW);
                FreeImage.Unload(p1);
                FreeImage.Unload(p32);
            }
            {
                String   fn  = "4bpp-PACKBITS";
                FIBITMAP p32 = FreeImage.CreateFromBitmap(Draws(fn, pic));
                FIBITMAP p1  = FreeImage.ConvertColorDepth(p32, FREE_IMAGE_COLOR_DEPTH.FICD_04_BPP);
                FreeImage.Save(FREE_IMAGE_FORMAT.FIF_TIFF, p1, fn + ".tif", FREE_IMAGE_SAVE_FLAGS.TIFF_PACKBITS);
                FreeImage.Unload(p1);
                FreeImage.Unload(p32);
            }
            {
                String   fn  = "4bpp-ADOBE_DEFLATE";
                FIBITMAP p32 = FreeImage.CreateFromBitmap(Draws(fn, pic));
                FIBITMAP p1  = FreeImage.ConvertColorDepth(p32, FREE_IMAGE_COLOR_DEPTH.FICD_04_BPP);
                FreeImage.Save(FREE_IMAGE_FORMAT.FIF_TIFF, p1, fn + ".tif", FREE_IMAGE_SAVE_FLAGS.TIFF_ADOBE_DEFLATE);
                FreeImage.Unload(p1);
                FreeImage.Unload(p32);
            }



            {
                String   fn  = "8bpp-DEFLATE";
                FIBITMAP p32 = FreeImage.CreateFromBitmap(Draws(fn, pic));
                FIBITMAP p1  = FreeImage.ConvertColorDepth(p32, FREE_IMAGE_COLOR_DEPTH.FICD_08_BPP);
                FreeImage.Save(FREE_IMAGE_FORMAT.FIF_TIFF, p1, fn + ".tif", FREE_IMAGE_SAVE_FLAGS.TIFF_DEFLATE);
                FreeImage.Unload(p1);
                FreeImage.Unload(p32);
            }
            {
                String   fn  = "8bpp-LZW";
                FIBITMAP p32 = FreeImage.CreateFromBitmap(Draws(fn, pic));
                FIBITMAP p1  = FreeImage.ConvertColorDepth(p32, FREE_IMAGE_COLOR_DEPTH.FICD_08_BPP);
                FreeImage.Save(FREE_IMAGE_FORMAT.FIF_TIFF, p1, fn + ".tif", FREE_IMAGE_SAVE_FLAGS.TIFF_LZW);
                FreeImage.Unload(p1);
                FreeImage.Unload(p32);
            }
            {
                String   fn  = "8bpp-PACKBITS";
                FIBITMAP p32 = FreeImage.CreateFromBitmap(Draws(fn, pic));
                FIBITMAP p1  = FreeImage.ConvertColorDepth(p32, FREE_IMAGE_COLOR_DEPTH.FICD_08_BPP);
                FreeImage.Save(FREE_IMAGE_FORMAT.FIF_TIFF, p1, fn + ".tif", FREE_IMAGE_SAVE_FLAGS.TIFF_PACKBITS);
                FreeImage.Unload(p1);
                FreeImage.Unload(p32);
            }
            {
                String   fn  = "8bpp-ADOBE_DEFLATE";
                FIBITMAP p32 = FreeImage.CreateFromBitmap(Draws(fn, pic));
                FIBITMAP p1  = FreeImage.ConvertColorDepth(p32, FREE_IMAGE_COLOR_DEPTH.FICD_08_BPP);
                FreeImage.Save(FREE_IMAGE_FORMAT.FIF_TIFF, p1, fn + ".tif", FREE_IMAGE_SAVE_FLAGS.TIFF_ADOBE_DEFLATE);
                FreeImage.Unload(p1);
                FreeImage.Unload(p32);
            }
            {
                String   fn  = "8bppGrayscale-JPEG";
                FIBITMAP p32 = FreeImage.CreateFromBitmap(Draws(fn, pic));
                FIBITMAP p1  = FreeImage.ConvertColorDepth(p32, FREE_IMAGE_COLOR_DEPTH.FICD_08_BPP | FREE_IMAGE_COLOR_DEPTH.FICD_FORCE_GREYSCALE);
                FreeImage.Save(FREE_IMAGE_FORMAT.FIF_TIFF, p1, fn + ".tif", FREE_IMAGE_SAVE_FLAGS.TIFF_JPEG);
                FreeImage.Unload(p1);
                FreeImage.Unload(p32);
            }



            {
                String   fn  = "24bpp-DEFLATE";
                FIBITMAP p32 = FreeImage.CreateFromBitmap(Draws(fn, pic));
                FIBITMAP p1  = FreeImage.ConvertColorDepth(p32, FREE_IMAGE_COLOR_DEPTH.FICD_24_BPP);
                FreeImage.Save(FREE_IMAGE_FORMAT.FIF_TIFF, p1, fn + ".tif", FREE_IMAGE_SAVE_FLAGS.TIFF_DEFLATE);
                FreeImage.Unload(p1);
                FreeImage.Unload(p32);
            }
            {
                String   fn  = "24bpp-LZW";
                FIBITMAP p32 = FreeImage.CreateFromBitmap(Draws(fn, pic));
                FIBITMAP p1  = FreeImage.ConvertColorDepth(p32, FREE_IMAGE_COLOR_DEPTH.FICD_24_BPP);
                FreeImage.Save(FREE_IMAGE_FORMAT.FIF_TIFF, p1, fn + ".tif", FREE_IMAGE_SAVE_FLAGS.TIFF_LZW);
                FreeImage.Unload(p1);
                FreeImage.Unload(p32);
            }
            {
                String   fn  = "24bpp-PACKBITS";
                FIBITMAP p32 = FreeImage.CreateFromBitmap(Draws(fn, pic));
                FIBITMAP p1  = FreeImage.ConvertColorDepth(p32, FREE_IMAGE_COLOR_DEPTH.FICD_24_BPP);
                FreeImage.Save(FREE_IMAGE_FORMAT.FIF_TIFF, p1, fn + ".tif", FREE_IMAGE_SAVE_FLAGS.TIFF_PACKBITS);
                FreeImage.Unload(p1);
                FreeImage.Unload(p32);
            }
            {
                String   fn  = "24bpp-ADOBE_DEFLATE";
                FIBITMAP p32 = FreeImage.CreateFromBitmap(Draws(fn, pic));
                FIBITMAP p1  = FreeImage.ConvertColorDepth(p32, FREE_IMAGE_COLOR_DEPTH.FICD_24_BPP);
                FreeImage.Save(FREE_IMAGE_FORMAT.FIF_TIFF, p1, fn + ".tif", FREE_IMAGE_SAVE_FLAGS.TIFF_ADOBE_DEFLATE);
                FreeImage.Unload(p1);
                FreeImage.Unload(p32);
            }
            {
                String   fn  = "24bpp-JPEG";
                FIBITMAP p32 = FreeImage.CreateFromBitmap(Draws(fn, pic));
                FIBITMAP p1  = FreeImage.ConvertColorDepth(p32, FREE_IMAGE_COLOR_DEPTH.FICD_24_BPP);
                FreeImage.Save(FREE_IMAGE_FORMAT.FIF_TIFF, p1, fn + ".tif", FREE_IMAGE_SAVE_FLAGS.TIFF_JPEG);
                FreeImage.Unload(p1);
                FreeImage.Unload(p32);
            }



            {
                String   fn = "32bpp-DEFLATE";
                FIBITMAP p1 = FreeImage.CreateFromBitmap(Draws(fn, pic));
                FreeImage.Save(FREE_IMAGE_FORMAT.FIF_TIFF, p1, fn + ".tif", FREE_IMAGE_SAVE_FLAGS.TIFF_DEFLATE);
                FreeImage.Unload(p1);
            }
            {
                String   fn = "32bpp-LZW";
                FIBITMAP p1 = FreeImage.CreateFromBitmap(Draws(fn, pic));
                FreeImage.Save(FREE_IMAGE_FORMAT.FIF_TIFF, p1, fn + ".tif", FREE_IMAGE_SAVE_FLAGS.TIFF_LZW);
                FreeImage.Unload(p1);
            }
            {
                String   fn = "32bpp-PACKBITS";
                FIBITMAP p1 = FreeImage.CreateFromBitmap(Draws(fn, pic));
                FreeImage.Save(FREE_IMAGE_FORMAT.FIF_TIFF, p1, fn + ".tif", FREE_IMAGE_SAVE_FLAGS.TIFF_PACKBITS);
                FreeImage.Unload(p1);
            }
            {
                String   fn = "32bpp-ADOBE_DEFLATE";
                FIBITMAP p1 = FreeImage.CreateFromBitmap(Draws(fn, pic));
                FreeImage.Save(FREE_IMAGE_FORMAT.FIF_TIFF, p1, fn + ".tif", FREE_IMAGE_SAVE_FLAGS.TIFF_ADOBE_DEFLATE);
                FreeImage.Unload(p1);
            }



            {
                String   fn = "8bppGrayscale-JPEG-ExtraSamples";
                FIBITMAP p1 = FreeImage.CreateFromBitmap(Draws(fn, pic));
                //FreeImage.Save(FREE_IMAGE_FORMAT.FIF_TIFF, p1, fn + ".tif", FREE_IMAGE_SAVE_FLAGS.TIFF_LZW);
                FreeImage.Unload(p1);
            }


            StringWriter wr = new StringWriter();

            foreach (String fp in Directory.GetFiles(".", "*.tif"))
            {
                wr.WriteLine(Resources.Templ
                             .Replace("*", Path.GetFileName(fp))
                             .Replace("?", Path.GetFileName(fp))
                             );
            }
            File.WriteAllText("Templ.txt", wr.ToString());
        }