public static BitmapEncoder ChooseEncoder(this ImageFormatEnum imageFormat) { switch (imageFormat) { case ImageFormatEnum.BMP: return(new BmpBitmapEncoder()); case ImageFormatEnum.GIF: return(new GifBitmapEncoder()); case ImageFormatEnum.JPEG: return(new JpegBitmapEncoder()); case ImageFormatEnum.PNG: return(new PngBitmapEncoder()); case ImageFormatEnum.TIFF: return(new TiffBitmapEncoder()); case ImageFormatEnum.WDP: return(new WmpBitmapEncoder()); default: throw new ArgumentOutOfRangeException("imageFormat", imageFormat, "No such encoder support for this imageFormat"); } }
/// <summary> /// Checking a specified physical path for image files (non recursive) /// </summary> /// <param name="path">The fully qualified path to check</param> /// <returns>A collection of ImageFile objects that containins each found image file's details.</returns> public ICollection <ImageFile> CheckPhysicalPath(string path) { List <ImageFile> imageFiles = new List <ImageFile>(); IEnumerable <string> filenames = fileRetriever.GetFiles(path); foreach (string filename in filenames) { byte[] fileBytes = File.ReadAllBytes(filename); ImageFormatEnum imgType = CheckHeader(fileBytes); if (imgType == ImageFormatEnum.Unknown) { continue; } using (var s = new MemoryStream(fileBytes)) using (Bitmap img = new Bitmap(s)) { imageFiles.Add(new ImageFile() { Content = fileBytes, FullPath = filename, Heigth = img.Height, ImageType = imgType, FileName = new FileInfo(filename).Name, Width = img.Width }); } } return(imageFiles); }
public void Convert(ImageFormatEnum destFormat, int compressionFactor) { ImageFormat systemFormat = ImageFormats.ImageFormatToSystemFormat(destFormat); Image newImage = ImageTools.ConvertImage(fSystemImage, systemFormat, compressionFactor); UpdateSystemImage(newImage); }
private static void ExportTexture(Texture2D tex, string path, ImageFormatEnum format, object data, bool zeroAlpha) { string formatName = GetImageFormatName(format); using (Stream output = File.OpenWrite(path + formatName)) { TextureUtil.ExportTexture(tex, output, format, data, zeroAlpha); } }
/// <summary> /// Write image file to stream /// </summary> /// <param name="DPI">DPI = DPI.X, DPI.Y(Dots per inch)</param> public void WriteToStream(BitMatrix QrMatrix, ImageFormatEnum imageFormat, Stream stream, Point DPI) { BitmapSource bitmapSource = WriteToBitmapSource(QrMatrix, DPI); BitmapEncoder encoder = imageFormat.ChooseEncoder(); encoder.Frames.Add(BitmapFrame.Create(bitmapSource)); encoder.Save(stream); }
private static bool SupportsQuality(ImageFormatEnum format) { switch (format) { case ImageFormatEnum.JPG: return(true); case ImageFormatEnum.PNG: default: return(false); // lossless PNG } }
private static string GetImageFormatName(ImageFormatEnum format) { switch (format) { case ImageFormatEnum.JPG: return(".jpg"); case ImageFormatEnum.PNG: default: return(".png"); } }
private static ImageFormat GetImageFormat(ImageFormatEnum format) { switch (format) { case ImageFormatEnum.JPG: return(ImageFormat.Jpeg); case ImageFormatEnum.PNG: default: return(ImageFormat.Png); } }
public void WriteToStream(BitMatrix qrMatrix, ImageFormatEnum imageFormat, Stream stream) { DrawingSize dSize = ISize.GetSize(qrMatrix == null ? 21 : qrMatrix.Width); WriteableBitmap wBitmap = new WriteableBitmap(dSize.CodeWidth, dSize.CodeWidth, 96, 96, PixelFormats.Gray8, null); this.Draw(wBitmap, qrMatrix); BitmapEncoder encoder = imageFormat.ChooseEncoder(); encoder.Frames.Add(BitmapFrame.Create(wBitmap)); encoder.Save(stream); }
/// <summary> /// Compresses the image to a quality suitable for display on the web, and saves the /// compressed file to disk /// </summary> /// <param name="imageToSave"></param> /// <param name="savePath"></param> /// <param name="imageType"></param> /// <param name="imageFormat"></param> public void CompressAndSaveImage( Bitmap imageToSave, string savePath, ImageFormatEnum imageFormat, int quality ) { EncoderParameters encoderParams = new EncoderParameters(); long[] imageQuality = new long[1]; imageQuality[0] = quality; EncoderParameter encParameter = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, imageQuality); encoderParams.Param[0] = encParameter; ImageCodecInfo imageCodecInfo = GetImageCodecInfo( imageFormat ); imageToSave.Save(savePath, imageCodecInfo, encoderParams); imageToSave.Dispose(); }
/// <summary> /// Get the correct codec for this image based on the type of image that has been uploaded /// </summary> /// <param name="imageFormat"></param> /// <returns></returns> private ImageCodecInfo GetImageCodecInfo(ImageFormatEnum imageFormat ) { ImageCodecInfo[] imageCodecArray = ImageCodecInfo.GetImageEncoders(); ImageCodecInfo imageCodec; switch (imageFormat) { case ImageFormatEnum.bmp: imageCodec = imageCodecArray[0]; break; case ImageFormatEnum.jpeg: imageCodec = imageCodecArray[1]; break; case ImageFormatEnum.jpg: imageCodec = imageCodecArray[1]; break; case ImageFormatEnum.gif: imageCodec = imageCodecArray[2]; break; case ImageFormatEnum.tiff: imageCodec = imageCodecArray[3]; break; case ImageFormatEnum.png: imageCodec = imageCodecArray[4]; break; default: imageCodec = null; break; } return imageCodec; }
private void RecursivelyRetryDownload(string downloadURL, ref string tempFilePath, int count, int maxretry) { try { // download image if (downloadURL.Length <= 0) { return; } using (WebClient client = new WebClient()) { client.Headers.Add("user-agent", "JMM"); //OnImageDownloadEvent(new ImageDownloadEventArgs("", req, ImageDownloadEventType.Started)); //BaseConfig.MyAnimeLog.Write("ProcessImages: Download: {0} *** to *** {1}", req.URL, fullName); byte[] bytes = client.DownloadData(downloadURL); if (bytes.Length < 4) { throw new WebException( "The image download stream returned less than 4 bytes (a valid image has 2-4 bytes in the header)"); } ImageFormatEnum imageFormat = Misc.GetImageFormat(bytes); string extension; switch (imageFormat) { case ImageFormatEnum.bmp: extension = ".bmp"; break; case ImageFormatEnum.gif: extension = ".gif"; break; case ImageFormatEnum.jpeg: extension = ".jpeg"; break; case ImageFormatEnum.png: extension = ".png"; break; case ImageFormatEnum.tiff: extension = ".tiff"; break; default: throw new WebException("The image download stream returned an invalid image"); } if (extension.Length <= 0) { return; } string newFile = Path.ChangeExtension(tempFilePath, extension); if (newFile == null) { return; } if (File.Exists(newFile)) { File.Delete(newFile); } using (var fs = new FileStream(newFile, FileMode.Create, FileAccess.Write)) { fs.Write(bytes, 0, bytes.Length); } tempFilePath = newFile; } } catch (WebException) { if (count + 1 >= maxretry) { throw; } Thread.Sleep(500); RecursivelyRetryDownload(downloadURL, ref tempFilePath, count + 1, maxretry); } }
public static ImageFormat ImageFormatToSystemFormat(ImageFormatEnum type) { FormatDefinition fd = _formats.FirstOrDefault(x => x.Type == type); return(fd == null ? null : fd.SystemFormat); }
public static string ImageFormatToDescription(ImageFormatEnum type) { FormatDefinition fd = _formats.FirstOrDefault(x => x.Type == type); return(fd == null ? "Unknown" : fd.Description); }
public FormatDefinition(ImageFormatEnum type, string extension, string description, ImageFormat systemFormat) { Type = type; Extension = extension; Description = description; SystemFormat = systemFormat; }
/// <summary> /// Writes to stream. /// </summary> /// <param name="qrMatrix">The qr matrix.</param> /// <param name="imageFormat">The image format.</param> /// <param name="stream">The stream.</param> /// <remarks></remarks> public void WriteToStream(BitMatrix qrMatrix, ImageFormatEnum imageFormat, Stream stream) { DrawingSize dSize = ISize.GetSize(qrMatrix == null ? 21 : qrMatrix.Width); var wBitmap = new WriteableBitmap(dSize.CodeWidth, dSize.CodeWidth, 96, 96, PixelFormats.Gray8, null); Draw(wBitmap, qrMatrix); BitmapEncoder encoder = imageFormat.ChooseEncoder(); encoder.Frames.Add(BitmapFrame.Create(wBitmap)); encoder.Save(stream); }
public static string GetDescription(this ImageFormatEnum format) { return(format.ToString()); }
/// <summary> /// Write image file to stream /// Default DPI will be 96, 96 /// </summary> public void WriteToStream(BitMatrix QrMatrix, ImageFormatEnum imageFormat, Stream stream) { this.WriteToStream(QrMatrix, imageFormat, stream, new Point(96, 96)); }
private void OnGUI() { EditorGUILayout.BeginHorizontal(); EditorGUILayout.LabelField("Export Path"); exportPath = EditorGUILayout.TextField(exportPath); if (GUILayout.Button("...")) { // search for a folder exportPath = EditorUtility.OpenFolderPanel("JanusVR Export Folder", exportPath, @"C:\"); } EditorGUILayout.EndHorizontal(); defaultMeshFormat = (ExportMeshFormat)EditorGUILayout.EnumPopup("Default Mesh Format", defaultMeshFormat); defaultTexFormat = (ImageFormatEnum)EditorGUILayout.EnumPopup("Default Textures Format", defaultTexFormat); filterMode = (TextureFilterMode)EditorGUILayout.EnumPopup("Texture Filter", filterMode); defaultQuality = EditorGUILayout.IntSlider("Default Textures Quality", defaultQuality, 0, 100); uniformScale = EditorGUILayout.FloatField("Uniform Scale", uniformScale); exportMaterials = EditorGUILayout.Toggle("Export Materials", exportMaterials); exportSkybox = EditorGUILayout.Toggle("Export Skybox (6-sided)", exportSkybox); lightmapExportType = (LightmapExportType)EditorGUILayout.EnumPopup("Lightmap Type", lightmapExportType); if (lightmapExportType != LightmapExportType.None) { maxLightMapResolution = Math.Max(32, EditorGUILayout.IntField("Max Lightmap Resolution", maxLightMapResolution)); } if (!string.IsNullOrEmpty(exportPath)) { if (GUILayout.Button("Start Export")) { PreExport(); } } scrollPosition = GUILayout.BeginScrollView(scrollPosition); if (exported != null) { perTextureOptions = EditorGUILayout.Foldout(perTextureOptions, "Per Texture Options"); Rect last = GUILayoutUtility.GetLastRect(); last.y = last.y + last.height; float size = Math.Min(Screen.width * 0.3f, Screen.height * 0.1f); float half = Screen.width * 0.5f; if (perTextureOptions) { for (int i = 0; i < texturesExportedData.Count; i++) { TextureExportData tex = texturesExportedData[i]; Rect r = GUILayoutUtility.GetRect(Screen.width, size * 1.05f); GUI.DrawTexture(new Rect(size * 0.1f, r.y, size, size), tex.Preview); GUI.Label(new Rect(size * 1.1f, r.y, half - size, size), tex.Texture.name); float x1 = half * 1.3f; float y = r.y; float wid = half * 0.6f; GUI.Label(new Rect(half, y, half * 0.3f, last.height), "Format"); tex.Format = (ImageFormatEnum)EditorGUI.EnumPopup(new Rect(x1, y, wid, last.height), tex.Format); y += last.height; GUI.Label(new Rect(half, y, half * 0.3f, last.height), "Resolution"); tex.Resolution = EditorGUI.IntSlider(new Rect(x1, y, wid, last.height), tex.Resolution, 32, tex.Texture.width); y += last.height; GUI.Label(new Rect(half, y, half * 0.3f, last.height), "Export Alpha"); tex.ExportAlpha = EditorGUI.Toggle(new Rect(x1, y, wid, last.height), tex.ExportAlpha); y += last.height; if (SupportsQuality(tex.Format)) { GUI.Label(new Rect(half, y, half * 0.3f, last.height), "Quality"); tex.Quality = EditorGUI.IntSlider(new Rect(x1, y, wid, last.height), tex.Quality, 0, 100); } } } //perModelOptions = EditorGUILayout.Foldout(perModelOptions, "Per Model Options"); //if (perModelOptions) //{ // for (int i = 0; i < meshesExportedData.Count; i++) // { // MeshExportData model = meshesExportedData[i]; // string name = meshesNames[model.Mesh]; // Rect r = GUILayoutUtility.GetRect(Screen.width, size * 1.05f); // //GUI.DrawTexture(new Rect(size * 0.1f, r.y, size, size), model.Preview); // GUI.Label(new Rect(size * 1.1f, r.y, half - size, size), name); // GUI.Label(new Rect(half, r.y, half * 0.3f, last.height), "Format"); // model.Format = (ExportMeshFormat)EditorGUI.EnumPopup(new Rect(half * 1.3f, r.y, half * 0.7f, last.height), model.Format); // } //} if (GUILayout.Button("Do Export")) { DoExport(); } } GUILayout.EndScrollView(); }
private void SaveImageFile(double imageSize) { SaveFileDialog saveFileDIalog = new SaveFileDialog(); saveFileDIalog.Filter = @"PNG (.png)|*.png|Bitmap (.bmp)|*.bmp|JEPG (.jpg)|*.jpg|GIF (.gif)|*.gif|TIFF (.tiff)|*.tiff|WDP (.wdp)|*.wdp|EPS (.eps)|*.eps"; saveFileDIalog.DefaultExt = ".png"; saveFileDIalog.FileName = "QrCode"; saveFileDIalog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyPictures); if (saveFileDIalog.ShowDialog() == false) { return; } string fileName = saveFileDIalog.FileName; ImageFormatEnum imageFormat = ImageFormatEnum.PNG; string fileNameCheck = saveFileDIalog.FileName.ToLower(); if (fileNameCheck.EndsWith("png")) { imageFormat = ImageFormatEnum.PNG; } else if (fileNameCheck.EndsWith("bmp")) { imageFormat = ImageFormatEnum.BMP; } else if (fileNameCheck.EndsWith("jpg")) { imageFormat = ImageFormatEnum.JPEG; } else if (fileNameCheck.EndsWith("gif")) { imageFormat = ImageFormatEnum.GIF; } else if (fileNameCheck.EndsWith("tiff")) { imageFormat = ImageFormatEnum.TIFF; } else if (fileNameCheck.EndsWith("wdp")) { imageFormat = ImageFormatEnum.WDP; } else if (fileNameCheck.EndsWith("eps")) { Properties.Settings.Default.SaveSize = imageSize; Properties.Settings.Default.Save(); CreateEPSImage(imageSize, fileName); return; } else { return; } Properties.Settings.Default.SaveSize = imageSize; Properties.Settings.Default.Save(); int pixelSize = (int)(imageSize * 96); DrawingBrushRenderer dRenderer = new DrawingBrushRenderer(new FixedCodeSize(pixelSize, QuietZoneModules.Two), new SolidColorBrush(DarkColor), new SolidColorBrush(LightColor)); using (FileStream stream = new FileStream(fileName, FileMode.Create)) { dRenderer.WriteToStream(QrMatrix, imageFormat, stream); } }
public static void ExportTexture(Texture2D input, Stream output, ImageFormatEnum imageFormat, object data, bool zeroAlpha) { #if SYSTEM_DRAWING ImageFormat format = GetImageFormat(imageFormat); Color32[] colors = input.GetPixels32(); byte[] bdata = new byte[colors.Length * 4]; // this is slower than linear, but easier to fix the texture mirrored for (int x = 0; x < input.width; x++) { for (int y = 0; y < input.height; y++) { int index = x + (y * input.width); Color32 col = colors[index]; index = (x + ((input.height - y - 1) * input.width)) * 4; //index *= 4; bdata[index] = col.b; bdata[index + 1] = col.g; bdata[index + 2] = col.r; bdata[index + 3] = col.a; } } Bitmap bitmap = new Bitmap(input.width, input.height); BitmapData locked = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb); Marshal.Copy(bdata, 0, locked.Scan0, bdata.Length); bitmap.UnlockBits(locked); bitmap.Save(output, format); bitmap.Dispose(); #else Texture2D inp; if (zeroAlpha) { inp = ZeroAlpha(input); } else { inp = input; } byte[] exported; switch (imageFormat) { case ImageFormatEnum.JPG: exported = inp.EncodeToJPG((int)data); break; default: case ImageFormatEnum.PNG: exported = inp.EncodeToPNG(); break; } output.Write(exported, 0, exported.Length); if (zeroAlpha) { // destroy the texture UnityEngine.Object.DestroyImmediate(inp); } #endif }