// Get BitmapMetadata Write private InPlaceBitmapMetadataWriter getBitmapMetadataWrite() { InPlaceBitmapMetadataWriter mdtInplaceResult = null; if (testFileExists()) { try { this._fileStream = File.Open(this._filePath + this._fileName, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite); BitmapDecoder bitmapDecoder = new JpegBitmapDecoder(this._fileStream, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default); BitmapFrame bitmapFrame = bitmapDecoder.Frames[0]; mdtInplaceResult = bitmapFrame.CreateInPlaceBitmapMetadataWriter(); } catch (IOException err) { // Error this.addError("Une erreur est survenue lors de l'ouverture du fichier."); } } else { // Error this.addError("Le fichier: " + this._filePath + this._fileName + " n'existe pas"); } return(mdtInplaceResult); }
private void UpdateRotationMetadata(bool rotateLeft) { if (!IsLocalJpeg) { throw new InvalidOperationException("Can only rotate images loaded from local jpeg files."); } using (Stream imageStream = new FileStream(Uri.LocalPath, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite)) { BitmapFrame bitmapFrame = BitmapFrame.Create(imageStream); var metadataWriter = bitmapFrame.CreateInPlaceBitmapMetadataWriter(); int rotation = GetRotation(bitmapFrame.Metadata as BitmapMetadata); if (rotateLeft) { rotation = (rotation + 270) % 360; } else { rotation = (rotation + 90) % 360; } SetRotation(metadataWriter, rotation); metadataWriter.TrySave(); rotation = GetRotation(bitmapFrame.Metadata as BitmapMetadata); } }
private void CreateAndShowMainWindow() { // Create the application's main window mainWindow = new Window(); mainWindow.Title = "Image Metadata"; // <SnippetSetQuery> Stream pngStream = new System.IO.FileStream("smiley.png", FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite); PngBitmapDecoder pngDecoder = new PngBitmapDecoder(pngStream, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default); BitmapFrame pngFrame = pngDecoder.Frames[0]; InPlaceBitmapMetadataWriter pngInplace = pngFrame.CreateInPlaceBitmapMetadataWriter(); if (pngInplace.TrySave() == true) { pngInplace.SetQuery("/Text/Description", "Have a nice day."); } pngStream.Close(); // </SnippetSetQuery> // Draw the Image Image myImage = new Image(); myImage.Source = new BitmapImage(new Uri("smiley.png", UriKind.Relative)); myImage.Stretch = Stretch.None; myImage.Margin = new Thickness(20); // <SnippetGetQuery> // Add the metadata of the bitmap image to the text block. TextBlock myTextBlock = new TextBlock(); myTextBlock.Text = "The Description metadata of this image is: " + pngInplace.GetQuery("/Text/Description").ToString(); // </SnippetGetQuery> // Define a StackPanel to host Controls StackPanel myStackPanel = new StackPanel(); myStackPanel.Orientation = Orientation.Vertical; myStackPanel.Height = 200; myStackPanel.VerticalAlignment = VerticalAlignment.Top; myStackPanel.HorizontalAlignment = HorizontalAlignment.Center; // Add the Image and TextBlock to the parent Grid myStackPanel.Children.Add(myImage); myStackPanel.Children.Add(myTextBlock); // Add the StackPanel as the Content of the Parent Window Object mainWindow.Content = myStackPanel; mainWindow.Show(); }
public void SaveBitmap(Bitmap bitmap) { const string filetype = ".png"; string filename = DateTime.Today.Year.ToString() + "_" + DateTime.Today.Month.ToString() + "_" + DateTime.Today.Day.ToString(); filename = Path.Combine(screenshotFolder, filename); if (File.Exists(filename + filetype)) { var ExistingImage = Image.FromFile(filename + filetype); int n = 1; while (File.Exists(filename + "(" + n.ToString() + ")" + filetype)) { n++; } filename += "(" + n.ToString() + ")"; } //FileStream stream = new FileStream(filename + ".png", FileMode.CreateNew); var prop = (PropertyItem)FormatterServices.GetUninitializedObject(typeof(PropertyItem)); prop.Id = 37510; prop.Type = 2; prop.Value = Encoding.UTF8.GetBytes("Hello World!"); prop.Len = prop.Value.Length; bitmap.SetPropertyItem(prop); bitmap.Save(filename + filetype); BitmapMetadata data; //FileStream stream = new FileStream(filename + filetype, FileMode.Create); //BitmapMetadata myBitmapMetadata = new BitmapMetadata("jpeg"); //myBitmapMetadata.Comment = "this is a comment"; //var encoder3 = new JpegBitmapDecoder(stream, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.OnLoad); Stream pngStream = new FileStream(filename + filetype, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite); PngBitmapDecoder pngDecoder = new PngBitmapDecoder(pngStream, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default); BitmapFrame pngFrame = pngDecoder.Frames[0]; InPlaceBitmapMetadataWriter pngInplace = pngFrame.CreateInPlaceBitmapMetadataWriter(); if (pngInplace.TrySave() == true) { pngInplace.SetQuery("/Text/Description", "Have a nice day."); } pngStream.Close(); //ImageMetadata metadata = new ImageMetadata("blah","erik","fractal","this is a comment"); //SaveEXIFMetadata(bitmap, metadata, filename + ".jpeg"); //SaveEXIFMetadataProperty(bitmap,"hello", filename + ".png"); }
// ReSharper disable once InconsistentNaming public static void WriteIPTC(string filename, IIPTCModel iptc) { Console.WriteLine($"{iptc.ByLine} {iptc.Caption} {iptc.CopyrightNotice} {iptc.Headline} {iptc.Keywords}"); string filePath = Path.Combine(Constants.PicPath, filename); using (Stream fs = new FileStream(filePath, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite)) { JpegBitmapDecoder decoder = new JpegBitmapDecoder(fs, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default); BitmapFrame frame = decoder.Frames[0]; var metadata = frame.Metadata.Clone() as BitmapMetadata; InPlaceBitmapMetadataWriter jpgInPlace = frame.CreateInPlaceBitmapMetadataWriter(); if (jpgInPlace != null) { jpgInPlace.SetQuery(@"/app13/irb/8bimiptc/iptc/caption", iptc.Caption); jpgInPlace.SetQuery(@"/app13/irb/8bimiptc/iptc/keywords", iptc.Keywords); jpgInPlace.SetQuery(@"/app13/irb/8bimiptc/iptc/by-line", iptc.ByLine); jpgInPlace.SetQuery(@"/app13/irb/8bimiptc/iptc/copyright notice", iptc.CopyrightNotice); jpgInPlace.SetQuery(@"/app13/irb/8bimiptc/iptc/headline", iptc.Headline); } fs.Close(); } }
//ajouter des metadata au images public void AddTags(string filename, string[] tags) { //ouvre un stream pour l'image using (FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite)) { //decode l'image pour récupérer les metadata BitmapDecoder decoder = BitmapDecoder.Create(fs, BitmapCreateOptions.None, BitmapCacheOption.None); // saisi les frames bitmap, qui contiennent les metadata BitmapFrame frame = decoder.Frames[0]; // obtiens les metadata en tant que BitmapMetadata BitmapMetadata metadata = frame.Metadata as BitmapMetadata; // instancie InPlaceBitmapMetadataWriter pour écrire les metadata dans l'image InPlaceBitmapMetadataWriter writer = frame.CreateInPlaceBitmapMetadataWriter(); string[] keys; //si des metadata existent déjà if (metadata.Keywords != null) { //créé la liste complète des tags, vieux et nouveau keys = new string[metadata.Keywords.Count + tags.Length]; //récupère les valeurs des textbox var tag2 = TagWriter2.Text; int i = 0; //récupère la valeur des mots clé existant foreach (string keyword in metadata.Keywords) { keys[i] = keyword; i++; } // récupère la valeur des nouveaus mots clé foreach (string tag in tags) { keys[i] = tag; i++; } //écrit les nouvelles metadata dans l'image writer.SetQuery("System.Keywords", keys); writer.SetQuery("System.Author", tag2); } //si il n'y a pas de metadata existante else { keys = tags; //récupère les valeurs des textbox var Tag = TagWriter.Text; var tag2 = TagWriter2.Text; //écrit les nouvelles metadata dans l'image writer.SetQuery("System.Title", Tag); writer.SetQuery("System.Author", tag2); } //si il n'y a pas d'espace pour stocké les nouvelles metadata if (!writer.TrySave()) { fs.Close(); fs.Dispose(); string test = TagWriter.Text; SetUpMetadataOnImage(filename, test); } } }
private void OnSaveImpl( Document input, Stream output, SaveConfigToken token, Surface scratchSurface, ProgressEventHandler callback) { HDPhotoSaveConfigToken hdToken = token as HDPhotoSaveConfigToken; WmpBitmapEncoder wbe = new WmpBitmapEncoder(); using (RenderArgs ra = new RenderArgs(scratchSurface)) { input.Render(ra, true); } MemoryBlock block = scratchSurface.Scan0; IntPtr scan0 = block.Pointer; double dpiX; double dpiY; switch (input.DpuUnit) { case MeasurementUnit.Centimeter: dpiX = Document.DotsPerCmToDotsPerInch(input.DpuX); dpiY = Document.DotsPerCmToDotsPerInch(input.DpuY); break; case MeasurementUnit.Inch: dpiX = input.DpuX; dpiY = input.DpuY; break; case MeasurementUnit.Pixel: dpiX = Document.GetDefaultDpu(MeasurementUnit.Inch); dpiY = Document.GetDefaultDpu(MeasurementUnit.Inch); break; default: throw new InvalidEnumArgumentException(); } BitmapSource bitmapSource = BitmapFrame.Create( scratchSurface.Width, scratchSurface.Height, dpiX, dpiY, System.Windows.Media.PixelFormats.Bgra32, null, scan0, (int)block.Length, // TODO: does not support >2GB images scratchSurface.Stride); FormatConvertedBitmap fcBitmap = new FormatConvertedBitmap( bitmapSource, hdToken.BitDepth == 24 ? PixelFormats.Bgr24 : PixelFormats.Bgra32, null, 0); BitmapFrame outputFrame0 = BitmapFrame.Create(fcBitmap); wbe.Frames.Add(outputFrame0); wbe.ImageQualityLevel = (float)hdToken.Quality / 100.0f; string tempFileName = FileSystem.GetTempFileName(); FileStream tempFileOut = new FileStream(tempFileName, FileMode.Create, FileAccess.Write, FileShare.Read); wbe.Save(tempFileOut); tempFileOut.Close(); tempFileOut = null; FileStream tempFileIn = new FileStream(tempFileName, FileMode.Open, FileAccess.ReadWrite, FileShare.Read); WmpBitmapDecoder wbd = new WmpBitmapDecoder(tempFileIn, BitmapCreateOptions.None, BitmapCacheOption.None); BitmapFrame ioFrame0 = wbd.Frames[0]; InPlaceBitmapMetadataWriter metadata2 = ioFrame0.CreateInPlaceBitmapMetadataWriter(); CopyMetadataTo(metadata2, input.Metadata); tempFileIn.Close(); tempFileIn = null; FileStream tempFileIn2 = new FileStream(tempFileName, FileMode.Open, FileAccess.ReadWrite, FileShare.Read); Utility.CopyStream(tempFileIn2, output); tempFileIn2.Close(); tempFileIn2 = null; try { File.Delete(tempFileName); } catch (Exception) { } // WPF doesn't give us an IDisposable implementation on its types Utility.GCFullCollect(); }