Esempio n. 1
0
        public static MemoryStream SaveImage(Image img, EImageFormat imageFormat, int jpegQuality = 90, GIFQuality gifQuality = GIFQuality.Default)
        {
            MemoryStream stream = new MemoryStream();

            switch (imageFormat)
            {
            case EImageFormat.PNG:
                img.Save(stream, ImageFormat.Png);
                break;

            case EImageFormat.JPEG:
                img = ImageHelpers.FillBackground(img, Color.White);
                img.SaveJPG(stream, jpegQuality);
                break;

            case EImageFormat.GIF:
                img.SaveGIF(stream, gifQuality);
                break;

            case EImageFormat.BMP:
                img.Save(stream, ImageFormat.Bmp);
                break;

            case EImageFormat.TIFF:
                img.Save(stream, ImageFormat.Tiff);
                break;
            }

            return(stream);
        }
Esempio n. 2
0
        public static ImageData PrepareImage(Image img, TaskSettings taskSettings)
        {
            ImageData imageData = new ImageData();

            imageData.ImageFormat = taskSettings.ImageSettings.ImageFormat;

            if (taskSettings.ImageSettings.ImageFormat == EImageFormat.JPEG)
            {
                img = ImageHelpers.FillBackground(img, Color.White);
            }

            imageData.ImageStream = SaveImage(img, taskSettings.ImageSettings.ImageFormat, taskSettings);

            int sizeLimit = taskSettings.ImageSettings.ImageSizeLimit * 1000;

            if (taskSettings.ImageSettings.ImageFormat != taskSettings.ImageSettings.ImageFormat2 && sizeLimit > 0 && imageData.ImageStream.Length > sizeLimit)
            {
                if (taskSettings.ImageSettings.ImageFormat2 == EImageFormat.JPEG)
                {
                    img = ImageHelpers.FillBackground(img, Color.White);
                }

                imageData.ImageStream = SaveImage(img, taskSettings.ImageSettings.ImageFormat2, taskSettings);
                imageData.ImageFormat = taskSettings.ImageSettings.ImageFormat2;
            }

            return(imageData);
        }
Esempio n. 3
0
        public static string CreateThumbnail(Image img, string folder, string filename, TaskSettings taskSettings)
        {
            if ((taskSettings.ImageSettings.ThumbnailWidth > 0 || taskSettings.ImageSettings.ThumbnailHeight > 0) && (!taskSettings.ImageSettings.ThumbnailCheckSize ||
                                                                                                                      (img.Width > taskSettings.ImageSettings.ThumbnailWidth && img.Height > taskSettings.ImageSettings.ThumbnailHeight)))
            {
                string thumbnailFileName = Path.GetFileNameWithoutExtension(filename) + taskSettings.ImageSettings.ThumbnailName + ".jpg";
                string thumbnailFilePath = CheckFilePath(folder, thumbnailFileName, taskSettings);

                if (!string.IsNullOrEmpty(thumbnailFilePath))
                {
                    Image thumbImage = null;

                    try
                    {
                        thumbImage = (Image)img.Clone();
                        thumbImage = new Resize(taskSettings.ImageSettings.ThumbnailWidth, taskSettings.ImageSettings.ThumbnailHeight).Apply(thumbImage);
                        thumbImage = ImageHelpers.FillBackground(thumbImage, Color.White);
                        thumbImage.SaveJPG(thumbnailFilePath, 90);
                        return(thumbnailFilePath);
                    }
                    finally
                    {
                        if (thumbImage != null)
                        {
                            thumbImage.Dispose();
                        }
                    }
                }
            }

            return(null);
        }
Esempio n. 4
0
        public override Image Apply(Image img)
        {
            if (UseGradient)
            {
                return(ImageHelpers.FillBackground(img, Color, Color2, GradientType));
            }

            return(ImageHelpers.FillBackground(img, Color));
        }
Esempio n. 5
0
        public override Bitmap Apply(Bitmap bmp)
        {
            if (UseGradient && Gradient != null && Gradient.IsValid)
            {
                return(ImageHelpers.FillBackground(bmp, Gradient));
            }

            return(ImageHelpers.FillBackground(bmp, Color));
        }
Esempio n. 6
0
        private void btnGenerate_Click(object sender, EventArgs e)
        {
            if (lvImages.Items.Count > 0)
            {
                int    width          = (int)nudWidth.Value;
                int    height         = (int)nudHeight.Value;
                int    quality        = (int)nudQuality.Value;
                string outputFolder   = txtOutputFolder.Text;
                string outputFilename = txtOutputFilename.Text;

                Cursor = Cursors.WaitCursor;

                try
                {
                    foreach (ListViewItem lvi in lvImages.Items)
                    {
                        string filePath = lvi.Text;

                        if (File.Exists(filePath))
                        {
                            using (Bitmap bmp = ImageHelpers.LoadImage(filePath))
                            {
                                if (bmp != null)
                                {
                                    using (Bitmap thumbnail = ImageHelpers.CreateThumbnail(bmp, width, height))
                                    {
                                        string filename   = Path.GetFileNameWithoutExtension(filePath);
                                        string outputPath = Path.Combine(outputFolder, outputFilename.Replace("$filename", filename));
                                        outputPath = Path.ChangeExtension(outputPath, "jpg");

                                        using (Bitmap newImage = ImageHelpers.FillBackground(thumbnail, Color.White))
                                        {
                                            ImageHelpers.SaveJPEG(newImage, outputPath, quality);
                                        }
                                    }
                                }
                            }
                        }
                    }

                    Cursor = Cursors.Default;
                    MessageBox.Show(Resources.ThumbnailsSuccessfullyGenerated, Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                catch (Exception ex)
                {
                    DebugHelper.WriteException(ex);
                    Cursor = Cursors.Default;
                    ex.ShowError();
                }
            }
        }
Esempio n. 7
0
        public override Image Apply(Image img)
        {
            if (UseGradient)
            {
                if (UseCustomGradient && Gradient != null && Gradient.IsValid)
                {
                    return(ImageHelpers.FillBackground(img, Gradient));
                }
                else
                {
                    return(ImageHelpers.FillBackground(img, Color, Color2, GradientType));
                }
            }

            return(ImageHelpers.FillBackground(img, Color));
        }
Esempio n. 8
0
 private static void SaveImageAsJPEGStream(Image img, Stream stream, int jpegQuality)
 {
     try
     {
         img = (Image)img.Clone();
         img = ImageHelpers.FillBackground(img, Color.White);
         img.SaveJPG(stream, jpegQuality);
     }
     finally
     {
         if (img != null)
         {
             img.Dispose();
         }
     }
 }
Esempio n. 9
0
        public NotificationForm(int duration, int fadeDuration, ContentAlignment placement, Size size, NotificationFormConfig config)
        {
            InitializeComponent();
            Icon = ShareXResources.Icon;
            SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint, true);

            Duration     = duration;
            FadeDuration = fadeDuration;

            opacityDecrement = (float)fadeInterval / FadeDuration;

            ToastConfig = config;
            textFont    = new Font("Arial", 10);

            if (config.Image != null)
            {
                config.Image = ImageHelpers.ResizeImageLimit(config.Image, size);
                Color backgroundColor = ShareXResources.UseCustomTheme ? ShareXResources.Theme.BackgroundColor : SystemColors.Window;
                config.Image = ImageHelpers.FillBackground(config.Image, backgroundColor);
                size         = new Size(config.Image.Width + 2, config.Image.Height + 2);
            }
            else if (!string.IsNullOrEmpty(config.Text))
            {
                textRenderSize = TextRenderer.MeasureText(config.Text, textFont, size.Offset(-textPadding * 2), TextFormatFlags.Left | TextFormatFlags.EndEllipsis);
                size           = new Size(textRenderSize.Width + (textPadding * 2), textRenderSize.Height + (textPadding * 2) + 2);
            }

            Point position = Helpers.GetPosition(placement, new Point(windowOffset, windowOffset), Screen.PrimaryScreen.WorkingArea.Size, size);

            NativeMethods.SetWindowPos(Handle, (IntPtr)SpecialWindowHandles.HWND_TOPMOST, position.X + Screen.PrimaryScreen.WorkingArea.X,
                                       position.Y + Screen.PrimaryScreen.WorkingArea.Y, size.Width, size.Height, SetWindowPosFlags.SWP_NOACTIVATE);

            if (Duration <= 0)
            {
                DurationEnd();
            }
            else
            {
                tDuration.Interval = Duration;
                tDuration.Start();
            }
        }