private void btnOpen_Click( object sender, RoutedEventArgs e ) { CommonOpenFileDialog openDialog = new CommonOpenFileDialog(); openDialog.ShowPlacesList = true; openDialog.Multiselect = false; openDialog.IsFolderPicker = false; openDialog.AddToMostRecentlyUsedList = true; openDialog.Filters.Add( new CommonFileDialogFilter( "PNG images", "*.png" ) ); if ( openDialog.ShowDialog( this ) == CommonFileDialogResult.Ok ) { soureFilePath = openDialog.FileName; // get comment meta using ( FileStream fileStream = new FileStream( soureFilePath, FileMode.Open, FileAccess.Read ) ) { pngReader = new PngReader( fileStream ); // 参考自Hjg.Pngcs的SampleCustomChunk项目 // get last line: this forces loading all chunks pngReader.ReadChunksOnly(); tblkComment.Text = pngReader.GetMetadata().GetTxtForKey( Key_SemanticInfo ); pngReader.End(); fileStream.Close(); } image.BeginInit(); image.Source = new BitmapImage( new Uri( soureFilePath ) ); image.EndInit(); } }
public static void fatalError(String s, PngReader png1) { try { png1.End(); } catch (Exception ) { } throw new PngjException(s); }
private void btnSave_Click( object sender, RoutedEventArgs e ) { CommonSaveFileDialog saveDialog = new CommonSaveFileDialog(); saveDialog.ShowPlacesList = true; saveDialog.AddToMostRecentlyUsedList = true; saveDialog.Filters.Add( new CommonFileDialogFilter( "PNG images", "*.png" ) ); saveDialog.DefaultFileName = DateTime.Now.ToString( "yyyyMMddhhmmss" ) + ".png"; if ( saveDialog.ShowDialog( this ) == CommonFileDialogResult.Ok ) { using ( FileStream newFileStream = new FileStream( saveDialog.FileName, FileMode.OpenOrCreate, FileAccess.ReadWrite ), oldFileStream = new FileStream( soureFilePath, FileMode.Open, FileAccess.Read ) ) { string semanticInfo = @" sky: blue sky. heart: cloud heart. Meaning: It means love|爱|❤. "; ////// Reference: http://code.google.com/p/pngcs/wiki/Overview // get decoder pngReader = new PngReader( oldFileStream ); // create encoder pngWriter = new PngWriter( newFileStream, pngReader.ImgInfo ); // copy int chunkBehav = Hjg.Pngcs.Chunks.ChunkCopyBehaviour.COPY_ALL; // tell to copy all 'safe' chunks pngWriter.CopyChunksFirst( pngReader, chunkBehav ); // copy some metadata from reader int channels = pngReader.ImgInfo.Channels; if ( channels < 3 ) throw new Exception( "This example works only with RGB/RGBA images" ); for ( int row = 0; row < pngReader.ImgInfo.Rows; row++ ) { ImageLine l1 = pngReader.ReadRow( row ); // format: RGBRGB... or RGBARGBA... pngWriter.WriteRow( l1, row ); } pngWriter.CopyChunksLast( pngReader, chunkBehav ); // metadata after the image pixels? can happen // app info pngWriter.GetMetadata().SetText( Hjg.Pngcs.Chunks.PngChunkTextVar.KEY_Software, "Semantic Image" ); // semantic info Hjg.Pngcs.Chunks.PngChunk chunk = pngWriter.GetMetadata().SetText( Key_SemanticInfo, semanticInfo, false, false ); chunk.Priority = true; pngWriter.End(); // dont forget this pngReader.End(); oldFileStream.Close(); newFileStream.Close(); } } }