Welcome Guest! To enable all features, please Login or Register.

Notification

Icon
Error

Options
View
Last Go to last post Unread Go to first unread post
#1 Posted : Thursday, November 10, 2016 5:06:53 AM(UTC)

cks2k2  
cks2k2

Groups: Registered
Posts: 2


System: C# 4.0, Win7 x64, Leadtools v14 (old, I know)

Windows (GDI+) doesn't allow saving "true" mono Jpeg images (8bpp) - it will force them to 24-bit.

Our solution was to use our existing LeadTools libraries to save it as a true mono image via a MemoryStream obj.
It works, but we lost all the EXIF tags in the jpeg - this was added previously to the bitmap via Bitmap.SetPropertyItem().

So we thought we could just write back the tags using LeadTools' RasterCodecs.WriteComment() but after writing the comment out, we read nothing back out (null obj)

Code:

// _bitmap is a C# Bitmap obj w/ some PropertyItems previously set
Stream outputStream = new MemoryStream();
using(var rasterImg = new RasterImg(_bitmap))
{
  _rasterCodecs.Save(rasterImg, outputStream, RasterImageFormat.Jpeg, 8);
  
  // test adding some EXIF tags
  var rasterComment = new RasterCommentMetadata();
  rasterComment.Type = RasterCommentMetadataType.Artist;
  rasterComment.FromAscii("some test string");
  _rasterCodecs.WriteComment(outputStream, 1, rasterComment);

  // try read it out
  var testReadBack = _rasterCodecs.ReadComment(outputStream, 1, RasterCommentMetadataType.Artist);
  // testReadBack is null?
}


Please advise.
 

Try the latest version of LEADTOOLS for free for 60 days by downloading the evaluation: https://www.leadtools.com/downloads

Wanna join the discussion? Login to your LEADTOOLS Support accountor Register a new forum account.

#2 Posted : Tuesday, November 15, 2016 4:42:28 PM(UTC)

Hadi  
Hadi

Groups: Manager, Tech Support, Administrators
Posts: 197

Was thanked: 9 time(s) in 9 post(s)

The JPEG file format only supports certain tags as can be seen in the following documentation link:

https://www.leadtools.com/help/l...andleadfilecomments.html

If you are wanting to set RasterCommentMetadataType.Artist, you will need to save the image out as an ExifJpeg file since it supports the Artist tag:

https://www.leadtools.com/help/l...cs.exiffilecomments.html

Here is an example:

Code:
         using (Stream outputStream = new MemoryStream())
         using (RasterCodecs _rasterCodecs = new RasterCodecs())
         using (var rasterImg = _rasterCodecs.Load(file))
         {
            _rasterCodecs.Save(rasterImg, outputStream, RasterImageFormat.Exif, 8);

            var rasterComment = new RasterCommentMetadata();
            rasterComment.Type = RasterCommentMetadataType.Artist;
            rasterComment.FromAscii("some test string");
            _rasterCodecs.WriteComment(outputStream, 1, rasterComment);

            var testReadBack = _rasterCodecs.ReadComment(outputStream, 1, RasterCommentMetadataType.Artist);
            if (testReadBack != null)
               Console.WriteLine(testReadBack.ToAscii());

            Console.ReadLine();
         }


UserPostedImage
Hadi Chami
Developer Support Manager
LEAD Technologies, Inc.

LEAD Logo
 
#3 Posted : Tuesday, November 15, 2016 9:29:18 PM(UTC)

cks2k2  
cks2k2

Groups: Registered
Posts: 2


But I get a ArgumentException when calling rasterComment.FromAscii()?

Code:
Bitmap newBitmap = _bitmap.Clone() as Bitmap;
using(var rasterImg = new RasterImage(newBitmap))
{
  _rasterCodecs.Save(rasterImg, outputStream, RasterImageFormat.Jpeg, bpp);
  var rasterComment = new RasterCommentMetadata();
  rasterComment.Type = RasterCommentMetadataType.UserComment;
  rasterComment.FromAscii("some test string");
  _rasterCodecs.WriteComment(outputStream, 1, rasterComment);
}


Exception msg: Value does not fall within the expected range.
Stack trace:
at Leadtools.RasterCommentMetadata.a(RasterCommentMetadataDataType A_0)
at Leadtools.RasterCommentMetadata.FromAscii(String value)

Edited by moderator Wednesday, November 16, 2016 9:21:15 AM(UTC)  | Reason: Not specified

 
#4 Posted : Wednesday, November 16, 2016 10:27:37 AM(UTC)

Hadi  
Hadi

Groups: Manager, Tech Support, Administrators
Posts: 197

Was thanked: 9 time(s) in 9 post(s)

UserComment requires some extra work. The first 8 bytes must be one of the following:
User comment. Data type: RasterCommentMetadataDataType.Ascii. Length: variable. The first eight bytes specify the data format, and the remainder of the comment is in the specified format. The first eight bytes can be any of the following:
41 53 43 49 49 00 00 00 ASCII
4A 49 53 00 00 00 00 00 JIS (Japanese)
00 00 00 00 00 00 00 00 Undefined
https://www.leadtools.com/help/l...commentmetadatatype.html

Also, the example is using a byte array to set the comment:
https://www.leadtools.com/help/l...mentingexiffeatures.html

Also, you will need to target Exif format again instead of Jpeg as the Jpeg doesn't support these types of comments. Here is some sample code showing how this works:

Code:
using (Stream outputStream = new MemoryStream())
         using (RasterCodecs _rasterCodecs = new RasterCodecs())
         using (var rasterImg = _rasterCodecs.Load(file))
         {
            _rasterCodecs.Save(rasterImg, outputStream, RasterImageFormat.Exif, 8);

            var rasterComment = new RasterCommentMetadata();
            rasterComment.Type = RasterCommentMetadataType.UserComment;

            string comments = "This is a test User Comment";
            char[] data = comments.ToCharArray(0, comments.Length);
            byte[] dataComments = new byte[8 + comments.Length];

            // when you need to write a user comment the first 8 bytes 
            // must contain the "Ascii" word.
            dataComments[0] = 41;
            dataComments[1] = 53;
            dataComments[2] = 43;
            dataComments[3] = 49;
            dataComments[4] = 49;
            dataComments[5] = 0;
            dataComments[6] = 0;
            dataComments[7] = 0;

            for (int i = 0; i < comments.Length; i++)
               dataComments[8 + i] = (byte)data[i];

            rasterComment.FromByte(dataComments);
            _rasterCodecs.WriteComment(outputStream, 1, rasterComment);

            var testReadBack = _rasterCodecs.ReadComment(outputStream, 1, RasterCommentMetadataType.UserComment);
            if (testReadBack != null)
            {
               dataComments = testReadBack.GetData();
               string msg = "";
               for (int i = 0; i < (dataComments.Length - 8); i++)
                  msg += (char)dataComments[i + 8];

               Console.WriteLine(msg);
            }
            Console.ReadLine();
         }


Here is a screenshot of it working:
UserPostedImage
Hadi Chami
Developer Support Manager
LEAD Technologies, Inc.

LEAD Logo
 
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.

Powered by YAF.NET | YAF.NET © 2003-2019, Yet Another Forum.NET
This page was generated in 0.174 seconds.