Solved

Direct2D, Windows 7 and C#

Posted on 2012-04-12
9
786 Views
Last Modified: 2012-04-13
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!
0
Comment
Question by:Tom Knowlton
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
9 Comments
 
LVL 96

Accepted Solution

by:
Bob Learned earned 500 total points
ID: 37842654
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+.
0
 
LVL 5

Author Closing Comment

by:Tom Knowlton
ID: 37842695
I am abandoning this strategy.

Direct2D is too low level for our purposes.
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 37842705
May I ask about your "purposes"?  What are your requirements?
0
MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

 
LVL 5

Author Comment

by:Tom Knowlton
ID: 37842748
>>>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();
            pImage.Import(bImagem);
            PixTools.Annotation.AnnotationDocument annotationDoc = new PixTools.Annotation.AnnotationDocument();
            PixTools.Annotation.TextAnnotation textItem = new PixTools.Annotation.TextAnnotation();
            textItem.Text = new PixTools.Annotation.ItemText(AnnotationText);
            annotationDoc.Items.Add(textItem);
            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.
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 37842794
How many images would you need to annotate during a single run?
0
 
LVL 5

Author Comment

by:Tom Knowlton
ID: 37842809
>>>How many images would you need to annotate during a single run?

This is not known until runtime.  Typically, 4 to 6 images.
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 37842819
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?
0
 
LVL 5

Author Comment

by:Tom Knowlton
ID: 37842883
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.  :)
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 37843080
Have you profiled this to see what takes the most time in the 1 second?
0

Featured Post

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question