Solved

Metafile problem

Posted on 2004-08-15
12
1,988 Views
Last Modified: 2012-08-13
All i want to do is create a metafile.
This code:
System.Drawing.Imaging.Metafile m = new Metafile(@"C:\anywhere\anyfile.anyextension");

Gives me an error.  A messagebox pops up that says "A generic error has occurred in GDI+"

I can't even create a metafile from an imagestream.
What am I doing wrong?
0
Comment
Question by:davidastle
  • 5
  • 5
  • 2
12 Comments
 
LVL 37

Expert Comment

by:gregoryyoung
Comment Utility
Initializes a new instance of the Metafile class from the specified file name. .... is there actually a metafile there ?
0
 
LVL 2

Author Comment

by:davidastle
Comment Utility
No... how do i make a metafile then?  The msdn example at http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemdrawinggraphicsclassenumeratemetafiletopic2.asp just crashes when i run it giving the same "generic error has occurred in GDI+" error
0
 
LVL 2

Author Comment

by:davidastle
Comment Utility
MSDN says that "A metafile contains records that describe a sequence of graphics operations that can be recorded (constructed) and played back (displayed). Not inheritable."  Wonderful.  Its hard to record any sequence of graphics operations when you cant create an instance of a metafile!
0
 
LVL 37

Assisted Solution

by:gregoryyoung
gregoryyoung earned 400 total points
Comment Utility
[C#]
public void AddMetafileCommentBytes(PaintEventArgs e)
{
// Create temporary Graphics object for metafile
//  creation and get handle to its device context.
Graphics newGraphics = thisForm.CreateGraphics();
IntPtr hdc = newGraphics.GetHdc();
// Create metafile object to record.
Metafile metaFile1 = new Metafile("SampMeta.emf", hdc);
// Create graphics object to record metaFile.
Graphics metaGraphics = Graphics.FromImage(metaFile1);
// Draw rectangle in metaFile.
metaGraphics.DrawRectangle(new Pen(Color.Black, 5), 0, 0, 100, 100);
// Create comment and add to metaFile.
byte[] metaComment = {(byte)'T', (byte)'e', (byte)'s', (byte)'t'};
metaGraphics.AddMetafileComment(metaComment);
// Dispose of graphics object.
metaGraphics.Dispose();
// Dispose of metafile.
metaFile1.Dispose();
// Release handle to temporary device context.
newGraphics.ReleaseHdc(hdc);
// Dispose of scratch graphics object.
newGraphics.Dispose();
// Create existing metafile object to draw.
Metafile metaFile2 = new Metafile("SampMeta.emf");
// Draw metaFile to screen.
e.Graphics.DrawImage(metaFile2, new Point(0, 0));
// Dispose of metafile.
metaFile2.Dispose();
}

0
 
LVL 37

Expert Comment

by:gregoryyoung
Comment Utility
notice the passing of the hDc
0
 
LVL 2

Author Comment

by:davidastle
Comment Utility
Oh i get it.  Most of the constructors for the metafile are you used for playback.  If you want to make one for recording, you have to send in a device context to the graphics object that you want to record.

So now i want to create a metafile that records everything thats drawn in an OnPaint method.
When i try to save the metafile i create, it just gives another "generic error":

            protected override void OnPaint(PaintEventArgs e)
            {
                  if (createmetafile)
                  {
                        IntPtr hdc = e.Graphics.GetHdc();
                        this.myMeta = new Metafile(hdc, e.ClipRectangle);
                        createmetafile = false;
                        base.OnPaint (e);
                        e.Graphics.ReleaseHdc(hdc);
                        myMeta.Save(@"C:\a\MyMetafile.emf", ImageFormat.Emf);

                  }
                  else
                        base.OnPaint(e);
            }
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 5

Accepted Solution

by:
knowlton earned 100 total points
Comment Utility
Looks like you are on your way to a solution.

By way of information...there is an excellent book on GDI+, which includes a discussion of metafile construction:

(btw, the book was written by the author of www.c-sharpcorner.com):

Graphics Programming with GDI+  (Microsoft .NET Development Series) by Mahesh Chand

amazon:
http://www.amazon.com/exec/obidos/ASIN/0321160770/qid=1092615125/sr=ka-1/ref=pd_ka_1/103-6566618-7223010
0
 
LVL 37

Expert Comment

by:gregoryyoung
Comment Utility
from looking around at some posts i don't think you can actually save a metafile via image.save ...it appears to "just do it"

not sure how reliable this info is but ...
0
 
LVL 5

Expert Comment

by:knowlton
Comment Utility
In the above mentioned book see pp. 363 thru 370.

About saving...dunno.

I've never created metafiles...I just have a book that supposedly shows you how.  :)

Good luck!
0
 
LVL 2

Author Comment

by:davidastle
Comment Utility
Hmmm... well one last thing before I give you the points (i don't think i can expect a better answer).  I already know managed directX so learning GDI seems like a step down... is it worth the time?
0
 
LVL 37

Expert Comment

by:gregoryyoung
Comment Utility
I always give one point to people when discussing the relative goodness of solutions ...

Sometimes a hammer is good, sometimes a radial saw is good. Learning new/different technologies is no different than a carpenter buying a new tool. You may run into a case where your current tool may not be the optimum one, however in practice always use the tool that you feel is best according to the restrictions placed upon the project (be they technological or time based).
0
 
LVL 2

Author Comment

by:davidastle
Comment Utility
OK ill buy the book!  I'm going to keep this thread open for a few more days to see if anyone else has any ideas though.
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

771 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now