Direct2D, Windows 7 and C#

If I want better performance in doing a simple annotation, is it accurate to say that I should be using Direct2D instead of GDI+ under Windows 7?

I am writing a C# console application.  At one point, it is currently using System.Drawing.Graphics (which is GDI+ essentially) to do the annotation, like this:

   using (Graphics g = Graphics.FromImage(bm))
            using (SolidBrush solidWhiteBrush = new SolidBrush(Color.White))
            using (SolidBrush solidBlackBrush = new SolidBrush(Color.Black))
                // Write out annotation text
                g.FillRectangle(solidWhiteBrush, 0, 0, bm.Width, bm.Height);
                g.DrawString(AnnotationText, AnnotationFont, solidBlackBrush, new PointF(7f, 2f));



Open in new window

Is there a faster way to be doing this?

What would be the "Direct2D" way to write these 2 lines of code, specifically:

                g.FillRectangle(solidWhiteBrush, 0, 0, bm.Width, bm.Height);
                g.DrawString(AnnotationText, AnnotationFont, solidBlackBrush, new PointF(7f, 2f));

And what libraries do I need to include to get to the Direct2D methods?

Thank you!
Tom KnowltonWeb developerAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Bob LearnedCommented:
Hey, it looks like you already figured out how to annotate!!

I haven't compared DirectX to GDI+, but my guess is that DirectX would give you the performance over GDI+.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Tom KnowltonWeb developerAuthor Commented:
I am abandoning this strategy.

Direct2D is too low level for our purposes.
Bob LearnedCommented:
May I ask about your "purposes"?  What are your requirements?
Bootstrap 4: Exploring New Features

Learn how to use and navigate the new features included in Bootstrap 4, the most popular HTML, CSS, and JavaScript framework for developing responsive, mobile-first websites.

Tom KnowltonWeb developerAuthor Commented:
>>>May I ask about your "purposes"?  What are your requirements?

-It is a console app, so no winforms or GUI of any kind.
-Said console app is to take a series of TIFF images and annotate them (add text to the upper left corner of the TIFF image.

Here is the current code, which I want to optimize:

public MemoryStream Annotate(MemoryStream InputImage, string AnnotationText, Font AnnotationFont)
            InputImage.Position = 0;
            Bitmap bImagem = new Bitmap(InputImage);
            PixImage pImage = new PixImage();
            PixTools.Annotation.AnnotationDocument annotationDoc = new PixTools.Annotation.AnnotationDocument();
            PixTools.Annotation.TextAnnotation textItem = new PixTools.Annotation.TextAnnotation();
            textItem.Text = new PixTools.Annotation.ItemText(AnnotationText);
            Console.WriteLine("LEAD TOOLS ANNOTATE - no print top");
            PixImage newpi = pImage.MergeAnnotations(annotationDoc, MergeAttributes.MergeColorFormatPreserve, null);
            Bitmap anotherBitmap = newpi.GetBitmap();

            MemoryStream ms = new MemoryStream();
            anotherBitmap.Save(ms, ImageFormat.Tiff);

            ms.Position = 0;

            return ms;      


Open in new window

Having to create a Bitmap from the MemoryStream  (and at the end, the reverse happens) seems too "heavy" and inefficient.
Bob LearnedCommented:
How many images would you need to annotate during a single run?
Tom KnowltonWeb developerAuthor Commented:
>>>How many images would you need to annotate during a single run?

This is not known until runtime.  Typically, 4 to 6 images.
Bob LearnedCommented:
I was thinking that you needed to process 1000's of files, which would require careful attention to detail.  4 to 6 doesn't sound like you have a big need for optimization.  How fast is this running currently?
Tom KnowltonWeb developerAuthor Commented:
It is taking about 1 second per file.

But I should mention - we are in fact in a scalable environment.

The code should be written as if 1,000's of file might be processed.

Let's proceed as if that were true....if that is agreeable.  :)
Bob LearnedCommented:
Have you profiled this to see what takes the most time in the 1 second?
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Multimedia Programming

From novice to tech pro — start learning today.