private void Resize(string inputPath) { const int size = 150; const int quality = 75; using (var rawData = Image.FromFile(inputPath)) { //using (var image = new Bitmap(System.Drawing.Image.FromFile(inputPath))) using (var image = new Bitmap(rawData)) { int width, height; if (image.Width > image.Height) { width = size; height = Convert.ToInt32(image.Height * size / (double)image.Width); } else { width = Convert.ToInt32(image.Width * size / (double)image.Height); height = size; } var resized = new Bitmap(width, height); using (var graphics = Graphics.FromImage(resized)) { graphics.CompositingQuality = CompositingQuality.HighSpeed; graphics.InterpolationMode = InterpolationMode.HighQualityBicubic; graphics.CompositingMode = CompositingMode.SourceCopy; graphics.DrawImage(image, 0, 0, width, height); var filename = inputPath. Split(new string[] { "/", "\\" }, StringSplitOptions.RemoveEmptyEntries) .ToList() .Last() .Split(".") .First(); using (var output = File.Open(@$ "{picturePath}{filename}_small.jpg" , FileMode.Create)) { var qualityParamId = Encoder.Quality; var encoderParameters = new EncoderParameters(1); encoderParameters.Param[0] = new EncoderParameter(qualityParamId, quality); var codec = ImageCodecInfo.GetImageDecoders() .FirstOrDefault(codec => codec.FormatID == ImageFormat.Jpeg.Guid); resized.Save(output, System.Drawing.Imaging.ImageFormat.Jpeg); } } } } }
/// <summary> /// Converts to MimeType. /// Also can be used to validate RawFormat value /// </summary> /// <param name="item">Image to evaluate</param> /// <returns>String mime type</returns> public static string ToMimeType(this Image item) { var returnValue = MimeTypes.ImageUnknown; var itemGuid = item.RawFormat.Guid; foreach (ImageCodecInfo codec in ImageCodecInfo.GetImageDecoders()) { if (codec.FormatID == itemGuid) { return(codec.MimeType); } } return(returnValue); }
/// <summary> /// Returns the MIME type of the specified image. /// </summary> /// <param name="image">The image.</param> /// <returns>A mime type string.</returns> public static string GetMimeType(Image image) { Check.Require <ArgumentNullException>(image != null, "image"); foreach (ImageCodecInfo codec in ImageCodecInfo.GetImageDecoders()) { if (codec.FormatID == image.RawFormat.Guid) { return(codec.MimeType); } } return("image/unknown"); }
private async Task <string> ConvertToDataUri(string url) { MemoryStream ms = new MemoryStream(); using (var image = Image.FromStream(await httpClient.GetStreamAsync(url))) { var encoder = ImageCodecInfo.GetImageDecoders().Where(x => x.FormatID == ImageFormat.Jpeg.Guid).FirstOrDefault(); var encoderParameters = new EncoderParameters(1); encoderParameters.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, useDataUriQuality); image.Save(ms, encoder, encoderParameters); } return($"data:image/jpeg;base64,{Convert.ToBase64String(ms.ToArray())}"); }
//mwtoda służąca do kompresji plików przy zapisie do JPEG //jest to metoda zaproponowana przez Microsoft private ImageCodecInfo GetEncoder(ImageFormat format) { //dekodujemy kodeki z pomocą pętli foreach, wybieramy po kolei elementy z tablicy codecs ImageCodecInfo[] codecs = ImageCodecInfo.GetImageDecoders(); foreach (ImageCodecInfo codec in codecs) { //sprawdzenie poprawności dekodowania i formatu if (codec.FormatID == format.Guid) { return(codec); } } return(null); }
private static ImageCodecInfo GetEncoder(string formatTo) { ImageFormat format; switch (formatTo.ToUpper()) { case ".PNG": case "PNG": format = ImageFormat.Png; break; case ".TIFF": case "TIFF": format = ImageFormat.Tiff; break; case ".BMP": case "BMP": format = ImageFormat.Bmp; break; case ".JPEG": case "JPEG": case ".JPG": case "JPG": format = ImageFormat.Jpeg; break; case ".ICON": case "ICON": format = ImageFormat.Icon; break; default: format = ImageFormat.Jpeg; // default break; } ImageCodecInfo[] codecs = ImageCodecInfo.GetImageDecoders(); foreach (ImageCodecInfo codec in codecs) { if (codec.FormatID == format.Guid) { return(codec); } } return(null); }
private static ImageCodecInfo GetEncoder(ImageFormat format) { ImageCodecInfo[] imageDecoders = ImageCodecInfo.GetImageDecoders(); ImageCodecInfo[] array = imageDecoders; for (int i = 0; i < array.Length; i++) { ImageCodecInfo imageCodecInfo = array[i]; if (imageCodecInfo.FormatID == format.Guid) { return(imageCodecInfo); } } return(null); }
public static ImageCodecInfo EnkoderBul(ImageFormat ResimFormati) { ImageCodecInfo[] ICI = ImageCodecInfo.GetImageDecoders(); foreach (ImageCodecInfo i in ICI) { if (i.FormatID == ResimFormati.Guid) { return(i); } } return(null); }
public static ImageCodecInfo GetEncoder(this ImageFormat format) { var codecs = ImageCodecInfo.GetImageDecoders(); foreach (var codec in codecs) { if (codec.FormatID == GetNetImageFormat(format).Guid) { return(codec); } } return(null); }
/// <summary> /// システムがサポートする画像フォーマットの拡張子の配列を取得します。 /// </summary> /// <returns>読み込める拡張子のString配列</returns> public IEnumerable <string> GetSystemAcceptExt() { ImageCodecInfo[] decoders = ImageCodecInfo.GetImageDecoders(); foreach (ImageCodecInfo ici in decoders) { //セミコロン区切りで渡されるので string[] exts = ici.FilenameExtension.Split(new char[] { ';' }); foreach (string ext in exts) { yield return(ext); } } }
/// <summary> /// Attempt to identify MIME information directly from the bytes of an image. /// </summary> /// <param name="image">Hydrated image to get MIME information from</param> /// <returns>MIME information about the image</returns> /// <remarks>NOTE: Codecs must be installed on computer running this assembly or .Net will not be able to determine the correct MIME information</remarks> public MimeInfo this[Image image] { get { foreach (ImageCodecInfo codec in ImageCodecInfo.GetImageDecoders()) //EXTENSION Codec could be cached into a hashtable { if (codec.FormatID == image.RawFormat.Guid) { return(this[codec.FilenameExtension]); } } throw new IndexOutOfRangeException(string.Format("Cannot determine MimeInfo for '{0}'", image.RawFormat.ToString())); } }
private static string GetGDIPSupportedImages() { if (supportedImages == null) { ImageCodecInfo[] imageDecoders = ImageCodecInfo.GetImageDecoders(); StringBuilder builder = new StringBuilder(); foreach (ImageCodecInfo info in imageDecoders) { builder.Append(info.FilenameExtension + ";"); } supportedImages = builder.ToString().ToLower().Replace("*", string.Empty).Replace(".ico;", string.Empty); } return(supportedImages); }
private static ImageCodecInfo GetEncoder(ImageFormat format) { ImageCodecInfo[] codecs = ImageCodecInfo.GetImageDecoders(); foreach (ImageCodecInfo codec in codecs) { if (codec.FormatID == format.Guid) { return(codec); } } return(null); }
/// <summary> /// Finds the image decoder for a particular mimeType /// </summary> /// <param name="mimeType">MimeType that is needing decoded</param> /// <returns>Decoder, if found</returns> /// <exception cref="ArgumentException">Thrown when <paramref name="mimeType"/> is not found</exception> public static ImageCodecInfo Decoder(string mimeType) { int intCt; ImageCodecInfo[] aryEncoders = ImageCodecInfo.GetImageDecoders(); for (intCt = 0; intCt < aryEncoders.Length; intCt++) { if (aryEncoders[intCt].MimeType == mimeType) { return(aryEncoders[intCt]); } } throw new ArgumentException("MimeType '" + mimeType + "' not found"); }
public static ImageCodecInfo getEncoder(this ImageFormat format) { try { ImageCodecInfo[] codecs = ImageCodecInfo.GetImageDecoders(); foreach (ImageCodecInfo codec in codecs) { if (codec.FormatID == format.Guid) { return(codec); } } return(null); } catch { throw new Exception("Failed to get Encoder."); } }
public void LoadSave() { //using (OpenFileDialog d = new OpenFileDialog()) //{ //if (d.ShowDialog() == DialogResult.OK) //{ TargaImage image = new TargaImage(@"F:\3DContents\Modelos\Lightning\c001_01.tga"); MemoryStream ms = new MemoryStream(image.Image.Width * image.Image.Height * 32); var decoders = ImageCodecInfo.GetImageDecoders(); image.Image.Save(ms, ImageFormat.Bmp); File.WriteAllBytes("temp.bmp", ms.ToArray()); var bmpData = image.Image.LockBits(new System.Drawing.Rectangle(0, 0, image.Image.Width, image.Image.Height), ImageLockMode.ReadWrite, image.Image.PixelFormat); // Get the address of the first line. IntPtr ptr = bmpData.Scan0; // Declare an array to hold the bytes of the bitmap. int bytes = Math.Abs(bmpData.Stride) * image.Image.Height; byte[] rgbValues = new byte[bytes]; //Copy the RGB values into the array. //System.Runtime.InteropServices.Marshal.Copy(ptr, rgbValues, 0, bytes); // Set every third value to 255. A 24bpp bitmap will look red. //32bpp - BGRA for (int counter = 0; counter < rgbValues.Length; counter += 3) { rgbValues[counter] = 255; } // Copy the RGB values back to the bitmap System.Runtime.InteropServices.Marshal.Copy(rgbValues, 0, ptr, bytes); // Unlock the bits. image.Image.UnlockBits(bmpData); image.Image.Save("temp1.bmp", ImageFormat.Bmp); Process.Start("temp1.bmp"); //var g = ((TestApplication)(TestApplication.CurrentApplication)).Form.CreateGraphics(); //g.DrawImage(image.Image, 0, 0); //} //} }
public static ImageCodecInfo GetEncoder(ImageFormat format) { var codecs = ImageCodecInfo.GetImageDecoders(); foreach (var codec in codecs) { if (codec.FormatID == format.Guid) { return(codec); } } // Return return(null); }
public static byte[] ToCompressedBytes(this Image image) { EncoderParameters parameters = new EncoderParameters(1); parameters.Param[0] = new EncoderParameter(Encoder.Quality, _quality); var jpegCodec = ImageCodecInfo.GetImageDecoders() .First(c => c.FormatID == ImageFormat.Jpeg.Guid); using (var stream = new MemoryStream()) { image.Save(stream, jpegCodec, parameters); return(stream.ToArray()); } }
public static string GetExtensionFromImage(Bitmap image) { var format = image.RawFormat; var codec = ImageCodecInfo.GetImageDecoders().FirstOrDefault(c => c.FormatID == format.Guid); if (codec != null) { return(codec.FilenameExtension); } else { return(".png"); } }
/* * public static CvRect ToCvRect(Rectangle rectangle) * { * return new CvRect(rectangle.X, rectangle.Y, rectangle.Width, rectangle.Height); * } * * public static Rectangle ToRectangle(CvRect cvRect) * { * return new Rectangle(cvRect.X, cvRect.Y, cvRect.Width, cvRect.Height); * } */ public static string GetMimeTypeFromImage(Bitmap image) { var format = image.RawFormat; var codec = ImageCodecInfo.GetImageDecoders().FirstOrDefault(c => c.FormatID == format.Guid); if (codec != null) { return(codec.MimeType); } else { return("image/png"); } }
private static MemoryStream Compress(Stream input, int quality = 0) { MemoryStream output; Bitmap resized; try { using (var image = new Bitmap(input)) { int width = image.Width; int height = image.Height; resized = new Bitmap(width, height); using (var graphics = Graphics.FromImage(resized)) { graphics.CompositingQuality = CompositingQuality.HighSpeed; graphics.InterpolationMode = InterpolationMode.HighQualityBicubic; graphics.CompositingMode = CompositingMode.SourceCopy; graphics.DrawImage(image, 0, 0, width, height); output = new MemoryStream(); var qualityParamId = Encoder.Quality; var encoderParameters = new EncoderParameters(1); encoderParameters.Param[0] = new EncoderParameter(qualityParamId, quality); var codec = ImageCodecInfo.GetImageDecoders().FirstOrDefault(c => c.FormatID == ImageFormat.Jpeg.Guid); resized.Save(output, codec, encoderParameters); } } return(output); } catch (Exception ex) { throw ex; } finally { output = null; resized = null; } }
/// <summary> /// 根据图片高度压缩图片 /// </summary> /// <param name="InPath">图片路径</param> /// <param name="height">高度(px单位)</param> /// <param name="outPath">转移路径</param> /// <param name="flag">压缩质量(数字越小压缩率越高) 1-100</param> /// <returns></returns> public bool YaSuoFixedHeight(string InPath, int height, string outPath, int flag) { var iSource = (Bitmap)Image.FromFile(InPath); if (iSource == null) { return(false); } ImageFormat tFormat = iSource.RawFormat; EncoderParameters ep = new EncoderParameters(); long[] qy = new long[1]; qy[0] = flag; EncoderParameter eParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, qy); ep.Param[0] = eParam; try { ImageCodecInfo[] arrayICI = ImageCodecInfo.GetImageDecoders(); ImageCodecInfo jpegICIinfo = null; for (int x = 0; x < arrayICI.Length; x++) { if (arrayICI[x].FormatDescription.Equals("JPEG")) { jpegICIinfo = arrayICI[x]; break; } } string newext = outPath.Substring(outPath.LastIndexOf(".")).ToLower(); if (newext != ".jpeg" && newext != ".jpg") { jpegICIinfo = null; } if (jpegICIinfo != null) { iSource.Save(outPath, jpegICIinfo, ep); } else { iSource.Save(outPath, tFormat); } return(true); } catch { return(false); } iSource.Dispose(); }
public static string ShowImageDecoders() { StringBuilder sb = new StringBuilder(); ImageCodecInfo[] inflist = ImageCodecInfo.GetImageDecoders(); sb.Append("-------------------\r\n"); sb.Append("ImageDecoders Count: " + inflist.Length.ToString() + "\r\n"); foreach (ImageCodecInfo inf in inflist) { ShowCodecInfo(inf, sb); } return(sb.ToString()); }
public static byte[] ToByteArrayGdi(Bitmap resized, int quality) { using (var memoryStream = new MemoryStream()) { var codec = ImageCodecInfo.GetImageDecoders() .Where(c => c.FormatID == ImageFormat.Jpeg.Guid) .FirstOrDefault(); var encoderParams = new EncoderParameters(1); encoderParams.Param[0] = new EncoderParameter(Encoder.Quality, quality); if (codec != null) { resized.Save(memoryStream, codec, encoderParams); } return(memoryStream.ToArray()); } }
public ImageCodecInfoTests() { decoders = new Hashtable(); encoders = new Hashtable(); foreach (ImageCodecInfo decoder in ImageCodecInfo.GetImageDecoders()) { decoders[decoder.Clsid] = decoder; } foreach (ImageCodecInfo encoder in ImageCodecInfo.GetImageEncoders()) { encoders[encoder.Clsid] = encoder; } }
private static ImageCodecInfo GetEncoder(ImageType imageType) { var format = ToFormat(imageType); var codecs = ImageCodecInfo.GetImageDecoders(); foreach (var codec in codecs) { if (codec.FormatID == format.Guid) { return(codec); } } return(null); }
public ImageMerger(string saveFilePath) { saveFile = new FileInfo(saveFilePath); if (!saveFile.Directory.Exists) { throw new Exception("The directory path does not exist: " + saveFile); } parameters = new EncoderParameters(); parameters.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 80L); encoder = Array.Find <ImageCodecInfo>( ImageCodecInfo.GetImageDecoders(), codec => (codec.FormatID == ImageFormat.Jpeg.Guid)); }
private static ImageCodecInfo GetEncoder(ImageFormat format) { ImageCodecInfo[] codecs = ImageCodecInfo.GetImageDecoders(); ImageCodecInfo result = null; foreach (var codec in codecs) { if (codec.FormatID == format.Guid) { Console.WriteLine(codec.CodecName); result = codec; } } return(result); }
public void FixtureGetReady() { decoders = new Hashtable(); encoders = new Hashtable(); foreach (ImageCodecInfo decoder in ImageCodecInfo.GetImageDecoders()) { decoders[decoder.Clsid] = decoder; } foreach (ImageCodecInfo encoder in ImageCodecInfo.GetImageEncoders()) { encoders[encoder.Clsid] = encoder; } }
private ImageCodecInfo GetEncoder(ImageFormat format) { Neodynamic.SDK.Barcode.BarcodeProfessional.LicenseOwner = "Grupo Empresarial CT SA de CV-Ultimate Edition-OEM Developer License"; Neodynamic.SDK.Barcode.BarcodeProfessional.LicenseKey = "CWELTAJF8DTZP4RQEB6Y9RFPDH6EHE8J29PYSKF5B9LPZQHSMZJQ"; ImageCodecInfo[] codecs = ImageCodecInfo.GetImageDecoders(); foreach (ImageCodecInfo codec in codecs) { if (codec.FormatID == format.Guid) { return(codec); } } return(null); }