Exemplo n.º 1
0
        private void make_thm(int width, int height, bool fix_ar, string format)
        {
            Bitmap bmp = null;
            Graphics g = null;
            AviSynthReader reader = null;
            string new_script = m.script;
            string thmpath = Calculate.RemoveExtention(m.outfilepath) + format;

            SetLog("CREATING THM");
            SetLog("------------------------------");
            SetLog("Saving picture to: " + thmpath);
            SetLog(format.ToUpper() + " " + width + "x" + height + " \r\n");

            try
            {
                if (fix_ar)
                {
                    int crop_w = 0, crop_h = 0;
                    double old_asp = m.outaspect;
                    double new_asp = (double)width / (double)height;

                    if (old_asp < new_asp)
                    {
                        crop_h = Math.Max(Convert.ToInt32((m.outresh - ((m.outresh * old_asp) / new_asp)) / 2), 0);
                    }
                    else if (old_asp > new_asp)
                    {
                        crop_w = Math.Max(Convert.ToInt32((m.outresw - (m.outresw / old_asp) * new_asp) / 2), 0);
                    }

                    new_script += ("Lanczos4Resize(" + width + ", " + height + ", " + crop_w + ", " + crop_h + ", -" + crop_w + ", -" + crop_h + ")\r\n");
                }

                reader = new AviSynthReader(AviSynthColorspace.RGB24, AudioSampleType.Undefined);
                reader.ParseScript(new_script);

                //проверка на выходы за пределы общего количества кадров
                int frame = (m.thmframe > reader.FrameCount) ? reader.FrameCount / 2 : m.thmframe;

                if (width == reader.Width && height == reader.Height)
                {
                    bmp = new System.Drawing.Bitmap(reader.ReadFrameBitmap(frame));
                }
                else
                {
                    bmp = new Bitmap(width, height);
                    g = Graphics.FromImage(bmp);

                    //метод интерполяции при ресайзе
                    g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                    g.DrawImage(reader.ReadFrameBitmap(frame), 0, 0, width, height);
                }

                if (format == "jpg")
                {
                    //процент cжатия jpg
                    System.Drawing.Imaging.ImageCodecInfo[] info = System.Drawing.Imaging.ImageCodecInfo.GetImageEncoders();
                    System.Drawing.Imaging.EncoderParameters encoderParameters = new System.Drawing.Imaging.EncoderParameters(1);
                    encoderParameters.Param[0] = new System.Drawing.Imaging.EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 95L);

                    //jpg
                    bmp.Save(thmpath, info[1], encoderParameters);
                }
                else if (format == "png")
                {
                    //png
                    bmp.Save(thmpath, System.Drawing.Imaging.ImageFormat.Png);
                }
                else
                {
                    //bmp
                    bmp.Save(thmpath, System.Drawing.Imaging.ImageFormat.Bmp);
                }
            }
            catch (Exception ex)
            {
                SetLog("\r\nError creating THM: " + ex.Message.ToString() + Environment.NewLine);
            }
            finally
            {
                //завершение
                if (g != null) { g.Dispose(); g = null; }
                if (bmp != null) { bmp.Dispose(); bmp = null; }
                if (reader != null) { reader.Close(); reader = null; }
                SetLog("");
            }
        }
Exemplo n.º 2
0
        private void SavePicture(string path, int width, int height, bool fix_ar, int frame)
        {
            System.Drawing.Bitmap bmp = null;
            System.Drawing.Graphics g = null;
            AviSynthReader reader = null;
            string new_script = m.script;

            try
            {
                if (fix_ar && width != 0 && height != 0)
                {
                    int crop_w = 0, crop_h = 0;
                    double old_asp = m.outaspect;
                    double new_asp = (double)width / (double)height;

                    if (old_asp < new_asp)
                    {
                        crop_h = Math.Max(Convert.ToInt32((m.outresh - ((m.outresh * old_asp) / new_asp)) / 2), 0);
                    }
                    else if (old_asp > new_asp)
                    {
                        crop_w = Math.Max(Convert.ToInt32((m.outresw - (m.outresw / old_asp) * new_asp) / 2), 0);
                    }

                    new_script += ("Lanczos4Resize(" + width + ", " + height + ", " + crop_w + ", " + crop_h + ", -" + crop_w + ", -" + crop_h + ")\r\n");
                }

                reader = new AviSynthReader(AviSynthColorspace.RGB24, AudioSampleType.Undefined);
                reader.ParseScript(new_script);
                if (width == 0 || height == 0 || (width == reader.Width && height == reader.Height))
                {
                    bmp = new System.Drawing.Bitmap(reader.ReadFrameBitmap(frame));
                }
                else
                {
                    bmp = new System.Drawing.Bitmap(width, height);
                    g = System.Drawing.Graphics.FromImage(bmp);

                    //метод интерполяции при ресайзе
                    g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                    g.DrawImage(reader.ReadFrameBitmap(frame), 0, 0, width, height);
                }

                string ext = Path.GetExtension(path).ToLower();
                if (ext == ".jpg")
                {
                    //процент cжатия jpg
                    System.Drawing.Imaging.ImageCodecInfo[] info = System.Drawing.Imaging.ImageCodecInfo.GetImageEncoders();
                    System.Drawing.Imaging.EncoderParameters encoderParameters = new System.Drawing.Imaging.EncoderParameters(1);
                    encoderParameters.Param[0] = new System.Drawing.Imaging.EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 95L);

                    //jpg
                    bmp.Save(path, info[1], encoderParameters);
                }
                else if (ext == ".png")
                {
                    //png
                    bmp.Save(path, System.Drawing.Imaging.ImageFormat.Png);
                }
                else
                {
                    //bmp
                    bmp.Save(path, System.Drawing.Imaging.ImageFormat.Bmp);
                }
            }
            catch (Exception ex)
            {
                ErrorException("SavePicture: " + ex.Message, ex.StackTrace);
            }
            finally
            {
                //завершение
                if (g != null) { g.Dispose(); g = null; }
                if (bmp != null) { bmp.Dispose(); bmp = null; }
                if (reader != null) { reader.Close(); reader = null; }
            }
        }