private static SaveFormat GetSaveFormat(ImageFormat imageFormat)
 {
     SaveFormat sf = SaveFormat.Unknown;
     if (imageFormat.Equals(ImageFormat.Png))
         sf = SaveFormat.Png;
     else if (imageFormat.Equals(ImageFormat.Jpeg))
         sf = SaveFormat.Jpeg;
     else if (imageFormat.Equals(ImageFormat.Tiff))
         sf = SaveFormat.Tiff;
     else if (imageFormat.Equals(ImageFormat.Bmp))
         sf = SaveFormat.Bmp;
     else
         sf = SaveFormat.Unknown;
     return sf;
 }
Beispiel #2
0
 public static void SaveBitmap(Bitmap bitmap, string fileName, ImageFormat imageFormat)
 {
     if (imageFormat.Equals(ImageFormat.Gif)) {
         SaveGif(bitmap, fileName);
     }
     else if (imageFormat.Equals(ImageFormat.Tiff)) {
         SaveTIFF(bitmap, fileName);
     }
     else if (imageFormat.Equals(ImageFormat.Jpeg)) {
         SaveJpeg(bitmap, fileName, 80);
     }
     else {
         bitmap.Save(fileName, imageFormat);
     }
 }
        private Byte GetTinytImageFormat(
            ImageFormat rawFormat)
        {
            if (rawFormat.Equals(ImageFormat.Gif))
            {
                return 1;
            }
            
            if (rawFormat.Equals(ImageFormat.Jpeg))
            {
                return 2;
            }

            return 0;
        }
Beispiel #4
0
        private static string GetMimeType(System.Drawing.Imaging.ImageFormat format)
        {
            if (format.Equals(ImageFormat.Bmp))
            {
                return("image/bmp");
            }
            if (format.Equals(ImageFormat.Gif))
            {
                return("image/gif");
            }
            if (format.Equals(ImageFormat.Jpeg))
            {
                return("image/jpeg");
            }
            if (format.Equals(ImageFormat.Tiff))
            {
                return("image/tiff");
            }
            if (format.Equals(ImageFormat.Png))
            {
                return("image/png");
            }
            if (format.Equals(ImageFormat.Icon))
            {
                return("image/x-icon");
            }
            if (format.Equals(ImageFormat.Wmf))
            {
                return("windows/metafile");
            }

            return("");
        }
Beispiel #5
0
		public static string GetImageMimeType (ImageFormat format)
		{
			string mimeType = "image/x-unknown";

			if (format.Equals (ImageFormat.Gif))
			{
				mimeType = "image/gif";
			}
			else if (format.Equals (ImageFormat.Jpeg))
			{
				mimeType = "image/jpeg";
			}
			else if (format.Equals (ImageFormat.Png))
			{
				mimeType = "image/png";
			}
			else if (format.Equals (ImageFormat.Bmp) || format.Equals (ImageFormat.MemoryBmp))
			{
				mimeType = "image/bmp";
			}
			else if (format.Equals (ImageFormat.Tiff))
			{
				mimeType = "image/tiff";
			}
			else if (format.Equals (ImageFormat.Icon))
			{
				mimeType = "image/x-icon";
			}

			return mimeType;
		}
Beispiel #6
0
 /// <summary>
 /// コンストラクタ。
 /// </summary>
 public ScreenShot()
 {
     _dir = Option.Instance().SaveDirectory;
     _fileFmt = Option.Instance().FileNameFormat;
     _imgFmt = Option.Instance().ImageFormat;
     // 拡張子を設定する。Jpegの場合は"jpg"、それ以外は名称をそのままに小文字に変換している。
     _ext = _imgFmt.Equals(ImageFormat.Jpeg) ? "jpg" : _imgFmt.ToString().ToLower();
     _matches = Rx.Matches(_fileFmt);
 }
Beispiel #7
0
 // Taken from http://www.java2s.com/Code/CSharp/File-Stream/ImageFormattoExtension.htm
 // And modified.
 public static string ImageFormatToExtension(ImageFormat imageFormat)
 {
     if (imageFormat.Equals(ImageFormat.Bmp))
     {
         return "BMP";
     }
     else if (imageFormat.Equals(ImageFormat.MemoryBmp))
     {
         return "BMP";
     }
     else if (imageFormat.Equals(ImageFormat.Wmf))
     {
         return "EMF";
     }
     else if (imageFormat.Equals(ImageFormat.Wmf))
     {
         return "WMF";
     }
     else if (imageFormat.Equals(ImageFormat.Gif))
     {
         return "GIF";
     }
     else if (imageFormat.Equals(ImageFormat.Jpeg))
     {
         return "JPG";
     }
     else if (imageFormat.Equals(ImageFormat.Png))
     {
         return "PNG";
     }
     else if (imageFormat.Equals(ImageFormat.Tiff))
     {
         return "TIF";
     }
     else if (imageFormat.Equals(ImageFormat.Exif))
     {
         return "EXF";
     }
     else if (imageFormat.Equals(ImageFormat.Icon))
     {
         return "ICO";
     }
     return "";
 }
Beispiel #8
0
        public override void Save(Stream stream, ImageFormat format)
        {
            Android.Graphics.Bitmap.CompressFormat compressFormat;
            if (format.Equals(ImageFormat.Jpeg))
            {
                compressFormat = Android.Graphics.Bitmap.CompressFormat.Jpeg;
            }
            else if (format.Equals(ImageFormat.Png))
            {
                compressFormat = Android.Graphics.Bitmap.CompressFormat.Png;
            }
            else
            {
                throw new ArgumentOutOfRangeException("format", format, "Only supported formats are JPEG and PNG");
            }

            var androidBitmap = (Android.Graphics.Bitmap) this;

            androidBitmap.Compress(compressFormat, 100, stream);
        }
Beispiel #9
0
 public static string ToContentType(ImageFormat imageFormat)
 {
     if (imageFormat.Equals(ImageFormat.Bmp)) return "image/bmp";
     if (imageFormat.Equals(ImageFormat.Gif)) return "image/gif";
     if (imageFormat.Equals(ImageFormat.Icon)) return "image/vnd.microsoft.icon";
     if (imageFormat.Equals(ImageFormat.Jpeg)) return "image/jpeg";
     if (imageFormat.Equals(ImageFormat.Png)) return "image/png";
     if (imageFormat.Equals(ImageFormat.Tiff)) return "image/tiff";
     if (imageFormat.Equals(ImageFormat.Wmf)) return "image/wmf";
     return "image/bmp";
 }
Beispiel #10
0
		public static ImageFormatInfo FromImageFormat(ImageFormat format)
		{
			if (format.Equals(ImageFormat.Bmp) || format.Equals(ImageFormat.MemoryBmp))
				return Bmp;
			if (format.Equals(ImageFormat.Emf))
				return Emf;
			if (format.Equals(ImageFormat.Wmf))
				return Wmf;
			if (format.Equals(ImageFormat.Exif))
				return Exif;
			if (format.Equals(ImageFormat.Gif))
				return Gif;
			if (format.Equals(ImageFormat.Icon))
				return Icon;
			if (format.Equals(ImageFormat.Jpeg))
				return Jpeg;
			if (format.Equals(ImageFormat.Png))
				return Png;
			if (format.Equals(ImageFormat.Tiff))
				return Tiff;
			return Bmp;
		}
Beispiel #11
0
 internal static string GetMimeType(ImageFormat imgFormat)
 {
     if (imgFormat.Equals(ImageFormat.Jpeg)) {
         return "image/jpeg";
     } else if (imgFormat.Equals(ImageFormat.Png)) {
         return "image/png";
     } else if (imgFormat.Equals(ImageFormat.Bmp)) {
         return "image/bmp";
     } else if (imgFormat.Equals(ImageFormat.Gif)) {
         return "image/gif";
     } else if (imgFormat.Equals(ImageFormat.Tiff)) {
         return "image/tiff";
     } else if (imgFormat.Equals(ImageFormat.Icon)) {
         return "image/x-icon";
     } else {
         return "image/";
     }
 }
Beispiel #12
0
 public ImageFormat GetImageFormat(ImageFormat format)
 {
     if (format.Equals(ImageFormat.Jpeg))
         return ImageFormat.Jpeg;
     if (format.Equals(ImageFormat.Bmp))
         return ImageFormat.Bmp;
     if (format.Equals(ImageFormat.Png))
         return ImageFormat.Png;
     if (format.Equals(ImageFormat.Emf))
         return ImageFormat.Emf;
     if (format.Equals(ImageFormat.Exif))
         return ImageFormat.Exif;
     if (format.Equals(ImageFormat.Gif))
         return ImageFormat.Gif;
     if (format.Equals(ImageFormat.Icon))
         return ImageFormat.Icon;
     if (format.Equals(ImageFormat.MemoryBmp))
         return ImageFormat.MemoryBmp;
     if (format.Equals(ImageFormat.Tiff))
         return ImageFormat.Tiff;
     return ImageFormat.Wmf;
 }
 private static string GetImgFormatName(ImageFormat format)
 {
     if (format.Equals(ImageFormat.Bmp)) return "bmp";
     if (format.Equals(ImageFormat.Emf)) return "emf";
     if (format.Equals(ImageFormat.Exif)) return "exif";
     if (format.Equals(ImageFormat.Gif)) return "gif";
     if (format.Equals(ImageFormat.Icon)) return "icon";
     if (format.Equals(ImageFormat.Jpeg)) return "jpeg";
     if (format.Equals(ImageFormat.Png)) return "png";
     if (format.Equals(ImageFormat.Tiff)) return "tiff";
     if (format.Equals(ImageFormat.Wmf)) return "wmf";
     return "jpg";
 }
Beispiel #14
0
 public static String MimeTypeFromImageFormat(ImageFormat format)
 {
     if (format.Equals(ImageFormat.Jpeg))
     {
         return MIME_JPEG;
     }
     else if (format.Equals(ImageFormat.Gif))
     {
         return MIME_GIF;
     }
     else if (format.Equals(ImageFormat.Bmp))
     {
         return MIME_BMP;
     }
     else if (format.Equals(ImageFormat.Tiff))
     {
         return MIME_TIFF;
     }
     else if (format.Equals(ImageFormat.Png))
     {
         return MIME_PNG;
     }
     else
     {
         throw new ArgumentException("Unsupported  image format '" + format + "'", "format");
     }
 }
Beispiel #15
0
        private static string[] _getImageRawFormatDescription(System.Drawing.Imaging.ImageFormat rawFormat)
        {
            string[] result = new string[2];

            if (rawFormat.Equals(System.Drawing.Imaging.ImageFormat.Jpeg))
            {
                result[0] = "Jpeg";
                result[1] = ".jpg";
            }
            else if (rawFormat.Equals(System.Drawing.Imaging.ImageFormat.Bmp))
            {
                result[0] = "Bmp";
                result[1] = ".bmp";
            }
            else if (rawFormat.Equals(System.Drawing.Imaging.ImageFormat.Png))
            {
                result[0] = "Png";
                result[1] = ".png";
            }
            else if (rawFormat.Equals(System.Drawing.Imaging.ImageFormat.Gif))
            {
                result[0] = "Gif";
                result[1] = ".gif";
            }
            else if (rawFormat.Equals(System.Drawing.Imaging.ImageFormat.Emf))
            {
                result[0] = "Emf";
                result[1] = ".image";
            }
            else if (rawFormat.Equals(System.Drawing.Imaging.ImageFormat.Exif))
            {
                result[0] = "Exif";
                result[1] = ".image";
            }
            else if (rawFormat.Equals(System.Drawing.Imaging.ImageFormat.Icon))
            {
                result[0] = "Icon";
                result[1] = ".image";
            }
            else if (rawFormat.Equals(System.Drawing.Imaging.ImageFormat.MemoryBmp))
            {
                result[0] = "MemoryBmp";
                result[1] = ".image";
            }
            else if (rawFormat.Equals(System.Drawing.Imaging.ImageFormat.Tiff))
            {
                result[0] = "Tiff";
                result[1] = ".image";
            }
            else if (rawFormat.Equals(System.Drawing.Imaging.ImageFormat.Wmf))
            {
                result[0] = "Wmf";
                result[1] = ".image";
            }
            else
            {
                result[0] = "Unknown";
                result[1] = ".image";
            }

            return(result);
        }
        /// <summary>
        /// Persist the specified image to disk at the specified path. If the directory to contain the file does not exist, it
        /// is automatically created.
        /// </summary>
        /// <param name="image">The image to persist to disk.</param>
        /// <param name="newFilePath">The full physical path, including the file name to where the image is to be stored. Ex: C:\mypics\cache\2008\May\flower.jpg</param>
        /// <param name="imageFormat">The file format for the image.</param>
        /// <param name="jpegQuality">The quality value to save JPEG images at. This is a value between 1 and 100. This parameter
        /// is ignored if the image format is not JPEG.</param>
        /// <exception cref="ArgumentNullException">Thrown when <paramref name="imageFormat" /> is null.</exception>
        public static void SaveImageToDisk(System.Drawing.Image image, string newFilePath, ImageFormat imageFormat, int jpegQuality)
        {
            if (imageFormat == null)
                throw new ArgumentNullException("imageFormat");

            VerifyDirectoryExistsForNewFile(newFilePath);

            if (imageFormat.Equals(ImageFormat.Jpeg))
                SaveJpgImageToDisk(image, newFilePath, jpegQuality);
            else
                SaveNonJpgImageToDisk(image, newFilePath, imageFormat);
        }
 /// <summary>
 /// Gets the image type representation for a mimetype
 /// </summary>
 /// <param name="format">Content mimetype</param>
 /// <returns>Image type</returns>
 private static ImagePartType GetImagePartType(ImageFormat format)
 {
     if (format.Equals(ImageFormat.Jpeg))
         return ImagePartType.Jpeg;
     else if (format.Equals(ImageFormat.Emf))
         return ImagePartType.Emf;
     else if (format.Equals(ImageFormat.Gif))
         return ImagePartType.Gif;
     else if (format.Equals(ImageFormat.Icon))
         return ImagePartType.Icon;
     else if (format.Equals(ImageFormat.Png))
         return ImagePartType.Png;
     else if (format.Equals(ImageFormat.Tiff))
         return ImagePartType.Tiff;
     else if (format.Equals(ImageFormat.Wmf))
         return ImagePartType.Wmf;
     else
         return ImagePartType.Bmp;
 }
        /// <summary>
        /// Gets the MIME type from the image format.
        /// </summary>
        /// <param name="format">The image format.</param>
        /// <returns>the right MIME type.</returns>
        internal string ImageFormatToMimeType(ImageFormat format)
        {
            if (null == format)
            {
                throw new ArgumentNullException("format");
            }

            if (format.Equals(ImageFormat.Bmp) || format.Equals(ImageFormat.MemoryBmp))
            {
                return "image/bmp";
            }
            else if (format.Equals(ImageFormat.Emf))
            {
                return "image/x-emf";
            }
            else if (format.Equals(ImageFormat.Gif))
            {
                return "image/gif";
            }
            else if (format.Equals(ImageFormat.Jpeg))
            {
                return "image/jpeg";
            }
            else if (format.Equals(ImageFormat.Png))
            {
                return "image/x-png";
            }
            else if (format.Equals(ImageFormat.Tiff))
            {
                return "image/tiff";
            }
            else if (format.Equals(ImageFormat.Wmf))
            {
                return "image/x-wmf";
            }

            throw new NotSupportedException();
        }
        private static byte[] Resize( Size newSize, bool keepAspectRatio, Image image, ImageFormat outputFormat )
        {
            if (image == null)
                return null;

            Size cropSize = newSize;
            Size orginalSize = image.Size;

            //Calculate scale/Crop ratios
            if (!keepAspectRatio)
            {
                float scaleWidth = (float)newSize.Width / orginalSize.Width;
                float scaleHeight = (float)newSize.Height / orginalSize.Height;

                //Not proportional scale
                if (scaleWidth != scaleHeight)
                {
                    if (scaleHeight > scaleWidth)
                    {
                        newSize.Width = (int)(scaleHeight * orginalSize.Width);
                    }
                    else
                    {
                        newSize.Height = (int)(scaleWidth * orginalSize.Height);
                    }
                }
                else
                {
                    keepAspectRatio = true;
                }
            }

            try
            {
                using (MemoryStream ms = new MemoryStream ())
                {
                    using (Bitmap bmPhoto = new Bitmap (newSize.Width, newSize.Height, PixelFormat.Format24bppRgb))
                    {
                        bmPhoto.SetResolution (image.HorizontalResolution, image.VerticalResolution);

                        if (outputFormat.Equals (ImageFormat.Png))
                            bmPhoto.MakeTransparent ();

                        using (Graphics grPhoto = Graphics.FromImage (bmPhoto))
                        {
                            grPhoto.InterpolationMode = InterpolationMode.HighQualityBicubic;
                            grPhoto.SmoothingMode = SmoothingMode.HighQuality;
                            grPhoto.PixelOffsetMode = PixelOffsetMode.HighQuality;
                            grPhoto.CompositingQuality = CompositingQuality.HighQuality;

                            if (outputFormat.Equals (ImageFormat.Png))
                                grPhoto.FillRectangle (Brushes.Transparent, 0, 0, newSize.Width, newSize.Height);
                            else
                                grPhoto.FillRectangle (Brushes.White, 0, 0, newSize.Width, newSize.Height);

                            // Use wrap mode in order to get rid of the unwanted border around the image
                            using (ImageAttributes wrapMode = new ImageAttributes ())
                            {
                                wrapMode.SetWrapMode (WrapMode.TileFlipXY);
                                Rectangle rectangle = new Rectangle (0, 0, newSize.Width, newSize.Height);
                                grPhoto.DrawImage (image, rectangle, 0, 0, image.Width, image.Height, GraphicsUnit.Pixel, wrapMode);
                            }
                        }

                        if (!keepAspectRatio && !cropSize.Equals (newSize))
                        {
                            int widthToCrop = Math.Max (0, (newSize.Width - cropSize.Width) / 2);
                            int heigthToCrop = Math.Max (0, (newSize.Height - cropSize.Height) / 2);

                            using (Bitmap bmpCrop = new Bitmap (cropSize.Width, cropSize.Height, PixelFormat.Format24bppRgb))
                            {
                                bmPhoto.SetResolution (image.HorizontalResolution, image.VerticalResolution);

                                if (outputFormat.Equals (ImageFormat.Png))
                                    bmpCrop.MakeTransparent ();

                                using (Graphics gfx = Graphics.FromImage (bmpCrop))
                                {
                                    Rectangle source = new Rectangle (widthToCrop, heigthToCrop, cropSize.Width, cropSize.Height);
                                    Rectangle target = new Rectangle (0, 0, bmpCrop.Width, bmpCrop.Height);

                                    if (outputFormat.Equals (ImageFormat.Png))
                                        gfx.FillRectangle (Brushes.Transparent, 0, 0, newSize.Width, newSize.Height);
                                    else
                                        gfx.FillRectangle (Brushes.White, 0, 0, newSize.Width, newSize.Height);

                                    gfx.DrawImage (bmPhoto, target, source, GraphicsUnit.Pixel);
                                    GetImageStream (outputFormat, bmpCrop, ms);
                                }
                            }
                        }
                        else
                        {
                            GetImageStream (outputFormat, bmPhoto, ms);
                        }
                    }
                    return ms.ToArray ();
                }
            }
            catch (Exception e)
            {
                throw;
            }
        }
 public bool IsDirectPrintingSupported(ImageFormat imageFormat) {
     bool isDirectPrintingSupported = false;
     if (imageFormat.Equals(ImageFormat.Jpeg) || imageFormat.Equals(ImageFormat.Png)) {
        int nEscape = imageFormat.Equals(ImageFormat.Jpeg) ? SafeNativeMethods.CHECKJPEGFORMAT : SafeNativeMethods.CHECKPNGFORMAT;
        int outData = 0;
        DeviceContext dc = CreateInformationContext(DefaultPageSettings);
        HandleRef hdc = new HandleRef(dc, dc.Hdc);
        try {
         isDirectPrintingSupported = SafeNativeMethods.ExtEscape(hdc, SafeNativeMethods.QUERYESCSUPPORT, Marshal.SizeOf(typeof(int)), ref nEscape, 0, out outData) > 0;
        }
        finally {
         dc.Dispose();
        }
     }
     return isDirectPrintingSupported;
 
 }
Beispiel #21
0
 /// <summary>
 /// Get MIME type corresponding to given <paramref name="format"/>.
 /// </summary>
 /// <param name="format"></param>
 /// <returns>MIME type of given <paramref name="format"/>.</returns>
 private static string GetImageMime(ImageFormat format)
 {
     if (format.Equals(ImageFormat.Gif))
         return "image/gif";
     else if (format.Equals(ImageFormat.Jpeg))
         return "image/jpeg";
     else if (format.Equals(ImageFormat.Png))
         return "image/png";
     else if (format.Equals(ImageFormat.Bmp))
         return "image/bmp";
     else if (format.Equals(ImageFormat.Icon))
         return "image/x-icon";
     else if (format.Equals(ImageFormat.Tiff))
         return "image/tiff";
     else if (format.Equals(ImageFormat.Emf))
         return "image/x-emf"; // Windows Metafile
     else if (format.Equals(ImageFormat.Wmf))
         return "image/x-wmf"; // Windows Metafile
     else if (format.Equals(ImageFormat.Exif))
         return "image/jpeg";    // should it be even handled ?
     else
         throw new NotImplementedException();
 }
Beispiel #22
0
        private string Devolver_Extension(ImageFormat formato)
        {
            if (formato.Equals(ImageFormat.Jpeg))
                return ".jpg";

            if (formato.Equals(ImageFormat.Bmp))
                return ".bmp";

            if (formato.Equals(ImageFormat.Gif))
                return ".gif";

            if (formato.Equals(ImageFormat.Png))
                return ".png";

            return null;
        }
    public static byte[] EncodeImage(Image image, ImageFormat format, int quality)
    {
      EncoderParameters encoderParams = null;

      if(format.Equals(System.Drawing.Imaging.ImageFormat.Jpeg))
      {
        // quality is used by Jpeg and probably ignored by other formats
        encoderParams = new EncoderParameters(1);
        encoderParams.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, quality);
      }

      return EncodeImage(image, format, encoderParams);
    }
 public bool IsDirectPrintingSupported(ImageFormat imageFormat)
 {
     bool flag = false;
     if (imageFormat.Equals(ImageFormat.Jpeg) || imageFormat.Equals(ImageFormat.Png))
     {
         int inData = imageFormat.Equals(ImageFormat.Jpeg) ? 0x1017 : 0x1018;
         int outData = 0;
         DeviceContext wrapper = this.CreateInformationContext(this.DefaultPageSettings);
         HandleRef hDC = new HandleRef(wrapper, wrapper.Hdc);
         try
         {
             flag = SafeNativeMethods.ExtEscape(hDC, 8, Marshal.SizeOf(typeof(int)), ref inData, 0, out outData) > 0;
         }
         finally
         {
             wrapper.Dispose();
         }
     }
     return flag;
 }
        /// <summary>
        /// Gets the exif.
        /// </summary>
        /// <param name="bytes">The bytes.</param>
        /// <param name="format">The format.</param>
        /// <returns>System.Byte[][].</returns>
        public byte[] SetOrientation(byte[] bytes, ImageFormat format)
        {
            if (format.Equals(ImageFormat.Jpeg))
            {
                var orientation = new ImageOrientation(bytes);
                bytes = orientation.OrientImage();
            }

            return bytes;
        }
Beispiel #26
0
        public override System.IO.Stream GetStream()
        {
            if (RawFormat.Equals(System.Drawing.Imaging.ImageFormat.Jpeg))
            {
                ContentType = "image/jpeg";
            }
            else if (RawFormat.Equals(System.Drawing.Imaging.ImageFormat.Gif))
            {
                ContentType = "image/gif";
            }
            else if (RawFormat.Equals(System.Drawing.Imaging.ImageFormat.Bmp))
            {
                ContentType = "image/bmp";
            }
            else if (RawFormat.Equals(System.Drawing.Imaging.ImageFormat.Tiff))
            {
                ContentType = "image/tiff";
            }
            else if (RawFormat.Equals(System.Drawing.Imaging.ImageFormat.Png))
            {
                ContentType = "image/x-png";
            }
            else if (RawFormat.Equals(System.Drawing.Imaging.ImageFormat.Wmf))
            {
                ContentType = "image/wmf";
            }
            else
            {
                throw (new NotSupportedException("Unsupported image format \'" + RawFormat.ToString() + "\'"));
            }

            if (LowQuality && ContentType == "image/jpeg")
            {
                try
                {
                    ImageCodecInfo    myImageCodecInfo = GetEncoderInfo(ImageFormat.Jpeg);
                    Encoder           myEncoder;
                    EncoderParameter  myEncoderParameter;
                    EncoderParameters myEncoderParameters;


                    // Create an Encoder object based on the GUID
                    // for the Quality parameter category.
                    myEncoder = Encoder.Quality;

                    // Create an EncoderParameters object.
                    // An EncoderParameters object has an array of EncoderParameter
                    // objects. In this case, there is only one
                    // EncoderParameter object in the array.
                    myEncoderParameters = new EncoderParameters(1);


                    MemoryStream mem = new MemoryStream();

                    // Save the bitmap as a JPEG file with quality level 25.
                    myEncoderParameter           = new EncoderParameter(myEncoder, 25);
                    myEncoderParameters.Param[0] = myEncoderParameter;
                    Bitmap.Save(mem, myImageCodecInfo, myEncoderParameters);

                    //move position to begin
                    mem.Seek(0, SeekOrigin.Begin);
                    return(mem);
                }
                catch (Exception)
                {
                    MemoryStream mem = new MemoryStream();
                    Bitmap.Save(mem, RawFormat);

                    //move position to begin
                    mem.Seek(0, SeekOrigin.Begin);

                    return(mem);
                }
            }
            else
            {
                MemoryStream mem = new MemoryStream();
                Bitmap.Save(mem, RawFormat);

                //move position to begin
                mem.Seek(0, SeekOrigin.Begin);
                return(mem);
            }
        }
 // A method to help identify the image format type
 private string getReadableRawFormat(ImageFormat myFormat)
 {
     if (myFormat.Equals(System.Drawing.Imaging.ImageFormat.Jpeg))
         return "Jpeg";
     if (myFormat.Equals(System.Drawing.Imaging.ImageFormat.Bmp))
         return "Bmp";
     if (myFormat.Equals(System.Drawing.Imaging.ImageFormat.Png))
         return "Png";
     if (myFormat.Equals(System.Drawing.Imaging.ImageFormat.Gif))
         return "Gif";
     else
         return "Unrecognised";
 }
 private static void ProcessAndSaveImageIntoFolder_AndTestResults(string testImageName, ImageFormat expectedOutputFormat)
 {
     var inputPath = SIL.IO.FileLocator.GetFileDistributedWithApplication(_pathToTestImages, testImageName);
     using (var image = PalasoImage.FromFileRobustly(inputPath))
     {
         using (var folder = new TemporaryFolder())
         {
             var fileName = ImageUtils.ProcessAndSaveImageIntoFolder(image, folder.Path, false);
             Assert.AreEqual(expectedOutputFormat == ImageFormat.Jpeg ? ".jpg" : ".png", Path.GetExtension(fileName));
             var outputPath = folder.Combine(fileName);
             using (var img = Image.FromFile(outputPath))
             {
                 Assert.AreEqual(expectedOutputFormat, img.RawFormat);
             }
             var alternativeThatShouldNotBeThere = Path.Combine(Path.GetDirectoryName(outputPath),
                 Path.GetFileNameWithoutExtension(outputPath) + (expectedOutputFormat.Equals(ImageFormat.Jpeg) ? ".png" : ".jpg"));
             Assert.IsFalse(File.Exists(alternativeThatShouldNotBeThere),
                 "Did not expect to have the file " + alternativeThatShouldNotBeThere);
         }
     }
 }
Beispiel #29
0
 /// <summary>
 /// Get the <see cref="ImgType"/> corresponding to the given <see cref="ImageFormat"/>.
 /// </summary>
 /// <param name="format"></param>
 /// <returns>Corresponding PHP <see cref="ImgType"/> or <see cref="ImgType.Unknown"/>.</returns>
 internal static ImgType GetImgType(ImageFormat format)
 {
     if (format.Equals(ImageFormat.Gif))
         return ImgType.GIF;
     else if (format.Equals(ImageFormat.Jpeg))
         return ImgType.JPEG;
     else if (format.Equals(ImageFormat.Png))
         return ImgType.PNG;
     //else if (format.Equals(ImageFormat.Bmp))
     //    return ImgType.Unknown;
     else
         return ImgType.Unknown;
 }
Beispiel #30
0
		private void SaveImageSafely(string path, ImageFormat format)
		{
			//related to preserving pixelformat (bit depth):
			//	http://stackoverflow.com/questions/7276212/reading-preserving-a-pixelformat-format48bpprgb-png-bitmap-in-net
			if (File.Exists(path))
			{
				try
				{
					File.Delete(path);
				}
				catch (System.IO.IOException error)
				{
					throw new ApplicationException("The program could not replace the image " + path +
												   ", perhaps because this program or another locked it. Quit and try again. Then restart your computer and try again." + System.Environment.NewLine + error.Message);
				}
			}
			if (format.Equals(ImageFormat.Png) || format.Equals(ImageFormat.Bmp))
			{
				//The JPEG indirect saving below isn't needed for pngs and bmps, and
				// keeping it simple here prevents us from losing the bit depth of the original
				//reported in https://silbloom.myjetbrains.com/youtrack/issue/BL-2841
				Image.Save(path, format);
			}
			else
			{
				//nb: there are cases (notibly http://jira.palaso.org/issues/browse/WS-34711, after cropping a jpeg) where we get out of memory if we are not operating on a copy
				using (var image = new Bitmap(Image))
				{
					image.Save(path, format);
				}
			}
		}
        /// <summary>
        /// Converts an ImageFormat value to a Web Content Type
        /// </summary>
        /// <param name="formatGuid"></param>
        /// <returns></returns>
        public static string ImageFormatToContentType(ImageFormat format)
        {
            string ct = null;

            if (format.Equals(ImageFormat.Png))
                ct = "image/png";
            else if (format.Equals(ImageFormat.Jpeg))
                ct = "image/jpeg";
            else if (format.Equals(ImageFormat.Gif))
                ct = "image/gif";
            else if (format.Equals(ImageFormat.Tiff))
                ct = "image/tiff";
            else if (format.Equals(ImageFormat.Bmp))
                ct = "image/bmp";
            else if (format.Equals(ImageFormat.Icon))
                ct = "image/x-icon";
            else if (format.Equals(ImageFormat.Wmf))
                ct = "application/x-msmetafile";
            else
                throw new InvalidOperationException(string.Format(Resources.ERROR_UnableToConvertImageFormatToContentType, format.ToString()));

            return ct;
        }
 /// <summary>
 /// Get the format of the image.
 /// </summary>
 /// <param name="format">Format of the image.</param>
 /// <returns>
 /// A string with the name of the format - compression.
 /// </returns>
 private static string GetEncoderInfo(ImageFormat format) {
     while (true) {
         if (format.Equals(ImageFormat.Jpeg)) {
             return "JPEG";
         } else if (format.Equals(ImageFormat.Png)) {
             return "PNG";
         } else if (format.Equals(ImageFormat.Bmp)) {
             return "BMP";
         }
     }
 }
Beispiel #33
0
 private string GetContentType(ImageFormat imageFormat)
 {
     if (imageFormat.Equals(ImageFormat.Bmp))
     {
         return "image/bmp";
     }
     if (imageFormat.Equals(ImageFormat.Gif))
     {
         return "image/gif";
     }
     if (imageFormat.Equals(ImageFormat.Jpeg))
     {
         return "image/jpeg";
     }
     if (imageFormat.Equals(ImageFormat.Png))
     {
         return "image/png";
     }
     else
     {
         return "";
     }
 }
            public static string GetContentTypeByImageFormat(ImageFormat format) {
                string ctype = "image/x-unknown";

                if (format.Equals(ImageFormat.Gif)) {
                    ctype = "image/gif";
                } else if (format.Equals(ImageFormat.Jpeg)) {
                    ctype = "image/jpeg";
                } else if (format.Equals(ImageFormat.Png)) {
                    ctype = "image/png";
                } else if (format.Equals(ImageFormat.Bmp) || format.Equals(ImageFormat.MemoryBmp)) {
                    ctype = "image/bmp";
                } else if (format.Equals(ImageFormat.Icon)) {
                    ctype = "image/x-icon";
                } else if (format.Equals(ImageFormat.Tiff)) {
                    ctype = "image/tiff";
                }

                return ctype;
            }