Last post Oct 18, 2012 03:47 AM by zioturo
Oct 16, 2012 07:49 AM|DuncanGravill|LINK
I need to create a new image and assign a colour profile to it. Is this possible with System.Drawing.Bitmap?
It is easy to instantiate a new Bitmap but how can I assign a colour profile?
I have discovered that when loading an existing image some information about colour space is stored in the PropertyItems array property. Specifically it is property ID 40961
But it appears that it is not possible to instatiate a new PropertyItem as the PropertyItem type does not have a public constructor. So I am not allowed to create a new PropertyItem containing colour space information.
Is there a way of assigning a colour profile in System.Drawing?
Oct 16, 2012 11:06 AM|zioturo|LINK
The following code uses a "dummy" image to instantiate a new property item:
public static System.Drawing.Imaging.PropertyItem CreateNewImagePropertyItem(int tagId)
string imageBytesBase64 = "iVBORw0KGgoAAAANSUhEUgAAAAgAAAAIAQMAAAD+wSzIAAAAAXNSR0IArs4c6QAAAANQTFRF////p8QbyAAAAAlwSFlzAAALEwAACxMBAJqcGAAAAAd0SU1FB9oHFQc4MnCRFaoAAAAZdEVYdENvbW1lbnQAQ3JlYXRlZCB3aXRoIEdJTVBXgQ4XAAAAG0lEQVQIHQEQAO//AAAAAAAAAAAAAAAAAAAAAAAQAAFVtqlEAAAAAElFTkSuQmCC";
byte imageBytes = Convert.FromBase64String(imageBytesBase64);
using (System.IO.MemoryStream ms = new System.IO.MemoryStream(imageBytes))
using (System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(ms))
// Get the first property item
System.Drawing.Imaging.PropertyItem items = bitmap.PropertyItems;
System.Drawing.Imaging.PropertyItem result = items;
// Adjust the id
result.Id = tagId;
Then you can add the property item to your image...Example:
using (System.Drawing.Bitmap myImage = new System.Drawing.Bitmap(1024, 768))
// Create property 270 (image description)
System.Drawing.Imaging.PropertyItem myNewItem = CreateNewImagePropertyItem(270);
... Place your code here to set property item values ...
I Hope this helps you.
Oct 17, 2012 01:25 PM|DuncanGravill|LINK
I had already seen this as a possible solution. It just seems like a really awkward solution. What possible reason could Microsoft have for not allowing applications to instantiate PropertyItem objects? I could imagine that someone thought "why would anyone
want to create PropertyItems unless they were tampering with an image" so they decided to nanny everyone and not allow PropertyItems to be created. I often find I am thwarted in what I would like to do by design decisions Microsoft have made like this.
I think I will try Direct2D and C++ to see if I can make a start with that.
Oct 18, 2012 03:47 AM|zioturo|LINK
Yes, the logic behind these design decisions is very hard to understand.
Even the documentation of Image.SetPropertyItem method (
http://msdn.microsoft.com/en-US/library/system.drawing.image.setpropertyitem(v=vs.110).aspx ) suggests the same workaround ! (obtain a PropertyItem from another image...)