Example #1
0
        private void loadPreviewImage(string path)
        {
            btnPreview.Enabled = false;
            BackgroundWorker bw = new BackgroundWorker();

            bw.DoWork += new DoWorkEventHandler((xsender, xe) =>
            {
                if (pictureBox1.Image != null)
                {
                    pictureBox1.Image.Dispose();
                }

                if (pictureBox2.Image != null)
                {
                    pictureBox2.Image.Dispose();
                }

                if (previewImg != null)
                {
                    previewImg.Dispose();
                }

                var fi = new FileInfo(path);
                settings.lastPreviewDir = fi.DirectoryName;
                settings.Save();

                Log.Debug("Load file: {0}\n", path);
                previewPath = path;

                Bitmap tmp = new Bitmap(previewPath);
                int[] cst  = Gfx.GetConstraints(tmp.Width, tmp.Height, PREVIEW_MAX_WIDTH, PREVIEW_MAX_WIDTH);
                previewImg = Gfx.ScaleImage(tmp, cst[0], cst[1]);

                Bitmap img  = new Bitmap(previewImg);
                Bitmap img2 = new Bitmap(previewImg);

                tmp.Dispose();

                pictureBox1.SizeMode = PictureBoxSizeMode.Zoom;
                pictureBox1.Image    = (System.Drawing.Image)img;

                pictureBox2.SizeMode = PictureBoxSizeMode.Zoom;
                pictureBox2.Image    = (System.Drawing.Image)img2;
            });

            bw.RunWorkerCompleted += new RunWorkerCompletedEventHandler((xsender, xe) =>
            {
                Log.Debug("Loaded Done\n");
                btnPreview.Enabled = true;
            });

            Log.Debug("Loading image...");

            bw.RunWorkerAsync();
        }
Example #2
0
        /// <summary>
        /// Processes the file
        /// </summary>
        /// <returns></returns>
        public override bool Run()
        {
            //if (image == null)
            System.Drawing.Image image;
            image = (System.Drawing.Image)(new Bitmap(File.FullName, true));

            Log.Debug("  * Bearbetar fil: {0}...", File.Name);

            string new_name = getNewName(File);

            FileInfo nfi = new FileInfo(new_name);

            if (nfi.Exists)
            {
                try { System.IO.File.Delete(nfi.FullName); }
                catch (Exception e) {
                    Log.File("Kunde inte radera gammal kopia:\n    {0}\n", e.Message);
                    Log.File("    Nästa operation kommer förmodligen att misslyckas!\n");
                }
            }

            if (MaxWidth == 0 && MaxHeight == 0)
            {
                if (Attributes != null)
                {
                    Bitmap nimg;
                    using (nimg = Gfx.ApplyAttributes((Bitmap)image, Image.Attributes)) {
                        image.Dispose();
                        image = null;

                        EncoderParameters eparams = new EncoderParameters(1);
                        eparams.Param[0] =
                            new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, (long)95);
                        ImageFormat    fmt;
                        string         mime = GetMimeTypeForExtension(nfi.Extension, out fmt);
                        ImageCodecInfo ici  = Gfx.GetEncoderInfo(mime);

                        try {
                            if (ici != null)
                            {
                                nimg.Save(new_name, ici, eparams);
                            }
                            else
                            {
                                nimg.Save(new_name, fmt);
                            }

                            Log.Debug("klar\n");

                            nimg.Dispose();
                            nimg = null;
                        }
                        catch (Exception e) {
                            Log.File("    misslyckades!\n");
                            Log.File("    fel: {0}\n", e.Message);
                            FileInfo fi = new FileInfo(new_name);
                            if (fi.Exists)
                            {
                                try { System.IO.File.Delete(fi.FullName); }
                                catch { }
                            }

                            nimg.Dispose();
                            nimg = null;
                            return(false);
                        }
                    }
                }
            }
            else
            {
                int[] constraints = Gfx.GetConstraints(image.Width, image.Height,
                                                       MaxWidth, MaxHeight);
                Bitmap nimg;
                using (nimg = Gfx.ScaleImage((Bitmap)image, constraints[0],
                                             constraints[1])) {
                    //image = null;
                    image.Dispose();
                    image = null;

                    if (Image.Attributes != null)
                    {
                        nimg = Gfx.ApplyAttributes(nimg, Image.Attributes);
                    }

                    EncoderParameters eparams = new EncoderParameters(1);
                    eparams.Param[0] =
                        new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, (long)95);
                    ImageFormat    fmt;
                    string         mime = GetMimeTypeForExtension(nfi.Extension, out fmt);
                    ImageCodecInfo ici  = Gfx.GetEncoderInfo(mime);

                    try {
                        if (ici != null)
                        {
                            nimg.Save(new_name, ici, eparams);
                        }
                        else
                        {
                            nimg.Save(new_name, fmt);
                        }

                        Log.Debug("klar\n");

                        nimg.Dispose();
                        nimg = null;
                    }
                    catch (Exception e) {
                        Log.File("    misslyckades!\n");
                        Log.File("    fel: {0}\n", e.Message);
                        FileInfo fi = new FileInfo(new_name);
                        if (fi.Exists)
                        {
                            try { System.IO.File.Delete(fi.FullName); }
                            catch { }
                        }

                        nimg.Dispose();
                        nimg = null;
                        return(false);
                    }
                }
            }

            return(true);
        }