private void saveToolStripMenuItem_Click(object sender, EventArgs e) { if (imgManaged == null) { return; } System.Windows.Forms.SaveFileDialog dlg = new SaveFileDialog(); dlg.AddExtension = true; dlg.RestoreDirectory = true; dlg.Title = "Save image as..."; dlg.Filter = "Targa (*.tga)|*.tga|Jpeg2000 (*.j2c)|*.j2c|PNG (*.png)|*.png|Jpeg (*.jpg)|*.jpg|Bitmap (*.bmp)|*.bmp"; if (dlg.ShowDialog() == DialogResult.OK) { int type = dlg.FilterIndex; if (type == 2) { // jpeg200 File.WriteAllBytes(dlg.FileName, jpegdata); } else if (type == 1) { // targa File.WriteAllBytes(dlg.FileName, imgManaged.ExportTGA()); } else if (type == 3) { // png image.Save(dlg.FileName, ImageFormat.Png); } else if (type == 4) { // jpg image.Save(dlg.FileName, ImageFormat.Jpeg); } else { // BMP image.Save(dlg.FileName, ImageFormat.Bmp); } } dlg.Dispose(); dlg = null; }
/// <summary> /// Decode JPEG2000 data to an <seealso cref = "System.Drawing.Image" /> and /// <seealso cref = "ManagedImage" /> /// </summary> /// <param name = "encoded">JPEG2000 encoded data</param> /// <param name = "managedImage">ManagedImage object to decode to</param> /// <param name = "image">Image object to decode to</param> /// <returns>True if the decode succeeds, otherwise false</returns> public static bool DecodeToImage(byte[] encoded, out ManagedImage managedImage, out Image image) { managedImage = null; image = null; if (DecodeToImage(encoded, out managedImage)) { try { image = LoadTGAClass.LoadTGA(new MemoryStream(managedImage.ExportTGA())); return(true); } catch (Exception ex) { Logger.Log("Failed to export and load TGA data from decoded image", Helpers.LogLevel.Error, ex); return(false); } } else { return(false); } }
void SaveTextures() { OnProgress("Exporting textures..."); for (int i = 0; i < Textures.Count; i++) { var id = Textures[i]; if (TextureNames[i] == null) { OnProgress("Skipping " + id + " due to insufficient permissions"); continue; } string filename = TextureNames[i] + "." + ImageFormat.ToLower(); OnProgress("Fetching texture" + id); ManagedImage mImage = null; Image wImage = null; byte[] jpegData = null; try { System.Threading.AutoResetEvent gotImage = new System.Threading.AutoResetEvent(false); Client.Assets.RequestImage(id, ImageType.Normal, (state, asset) => { if (state == TextureRequestState.Finished && asset != null) { jpegData = asset.AssetData; OpenJPEG.DecodeToImage(jpegData, out mImage, out wImage); gotImage.Set(); } else if (state != TextureRequestState.Pending && state != TextureRequestState.Started && state != TextureRequestState.Progress) { gotImage.Set(); } }); gotImage.WaitOne(120 * 1000, false); if (wImage != null) { string fullFileName = System.IO.Path.Combine(System.IO.Path.GetDirectoryName(FileName), filename); switch (ImageFormat) { case "PNG": wImage.Save(fullFileName, System.Drawing.Imaging.ImageFormat.Png); break; case "JPG": wImage.Save(fullFileName, System.Drawing.Imaging.ImageFormat.Jpeg); break; case "BMP": wImage.Save(fullFileName, System.Drawing.Imaging.ImageFormat.Bmp); break; case "J2C": File.WriteAllBytes(fullFileName, jpegData); break; case "TGA": File.WriteAllBytes(fullFileName, mImage.ExportTGA()); break; default: throw new Exception("Unsupported image format"); } OnProgress("Saved to " + fullFileName); } else { throw new Exception("Failed to decode image"); } } catch (Exception ex) { OnProgress("Failed: " + ex); TextureNames[i] = null; } } }