?
Solved

Metafile problem

Posted on 2004-08-15
12
Medium Priority
?
2,049 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
[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
  • 5
  • 2
12 Comments
 
LVL 37

Expert Comment

by:gregoryyoung
ID: 11805440
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
ID: 11805762
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
ID: 11805770
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 37

Assisted Solution

by:gregoryyoung
gregoryyoung earned 1200 total points
ID: 11805817
[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
ID: 11805821
notice the passing of the hDc
0
 
LVL 2

Author Comment

by:davidastle
ID: 11805853
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
 
LVL 5

Accepted Solution

by:
Tom Knowlton earned 300 total points
ID: 11806251
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
ID: 11806254
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:Tom Knowlton
ID: 11806260
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
ID: 11806724
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
ID: 11806885
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
ID: 11808964
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Suggested Courses

777 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