コード例 #1
0
        public static void Convert(ConvertParameters parameters)
        {
            Stream inStream  = null;
            Stream outStream = null;

            try
            {
                using (var b = Platform.Instance.ThreadStart())
                {
                    inStream = parameters.InputStream;
                    if (inStream == null)
                    {
                        inStream = File.OpenRead(parameters.InputFileName);
                    }

                    var inFormat = GetFormat(parameters.InputFormat, parameters.InputFileName);
                    if (inFormat == null)
                    {
                        throw new PabloException(string.Format("Error: Cannot find format to use for file '{0}'", parameters.InputFileName));
                    }

                    var outFormat = GetFormat(parameters.OutputFormat, parameters.OutputFileName);
                    if (outFormat == null)
                    {
                        throw new PabloException(string.Format("Error: Cannot find format conversion from '{0}.{1}' format to file '{2}'", inFormat.Info.ID, inFormat.ID, parameters.OutputFileName));
                    }

                    using (var sourceDoc = inFormat.Info.Create(Platform.Instance))
                    {
                        sourceDoc.Info.SetOption("animation", "false");
                        using (var sourceHandler = sourceDoc.CreateHandler())
                        {
                            var formatId = sourceDoc.Info.OptionID ?? sourceDoc.Info.ID;
                            if (parameters.Options != null)
                            {
                                foreach (var option in sourceDoc.Info.Options)
                                {
                                    var optionId = string.Format("{0}-{1}", formatId, option.ID);
                                    var val      = parameters.Options.FirstOrDefault(r => string.Equals(r.Name, optionId, StringComparison.OrdinalIgnoreCase));
                                    if (val != null)
                                    {
                                        sourceDoc.Info.SetOption(option.ID, val.Value);
                                    }
                                }
                            }

                            sourceDoc.FileName = parameters.InputFileName;
                            sourceDoc.Load(inStream, inFormat, sourceHandler);

                            sourceHandler.Zoom = parameters.Zoom;
                            var drawSize = sourceHandler.DrawSize;
                            if (parameters.MaxWidth != null && drawSize.Width > parameters.MaxWidth.Value)
                            {
                                sourceHandler.Zoom = (float)parameters.MaxWidth.Value / (float)sourceHandler.Size.Width;
                            }

                            outStream = parameters.OutputStream;
                            if (outStream == null)
                            {
                                outStream = File.Create(parameters.OutputFileName);
                            }
                            sourceHandler.Save(outStream, outFormat);
                        }
                    }
                }
            }
            catch
            {
                // clean up file if conversion failed
                if (parameters.OutputStream == null && File.Exists(parameters.OutputFileName))
                {
                    if (outStream != null)
                    {
                        outStream.Close();
                        outStream.Dispose();
                        outStream = null;
                    }
                    File.Delete(parameters.OutputFileName);
                }
                throw;
            }
            finally
            {
                if (inStream != null && parameters.InputStream == null)
                {
                    inStream.Dispose();
                }
                if (outStream != null && parameters.OutputStream == null)
                {
                    outStream.Dispose();
                }
            }
        }
コード例 #2
0
ファイル: PabloEngine.cs プロジェクト: themnotyou/pablodraw
 public void Convert(ConvertParameters parameters)
 {
     EngineInternal.Convert(parameters);
 }