Search code examples
c#wpfimagepng

Save bitmap to png with medata data wpf c#


I need to save a bitmap to jpg with medata like copyright in wpf c#.

         var  handle = BitmapImage2Bitmap(currentimage).GetHbitmap();



        System.Drawing.Image img = System.Drawing.Image.FromHbitmap(handle);
        System.Drawing.Imaging.PropertyItem prop = img.PropertyItems[0];
        SetProperty(ref prop, 33432, "Copyright");
        img.SetPropertyItem(prop);

        


        img.Save(@"C:\images\test.jpg");

I already tried using the code above but it says that the index was out of the arrayarea.


Solution

  • var prop = img.GetPropertyItem(0x503B);
    prop.Value = Encoding.Ascii.GetBytes("copyright name");
    prop.Len = prop.Value.Length;
    img.SetPropertyItem(prop);
    

    The value 0x503B comes from here: PropertyItem.Id Property