Solved

Metafile problem

Posted on 2004-08-15
12
1,995 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
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
 
LVL 37

Assisted Solution

by:gregoryyoung
gregoryyoung earned 400 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 5

Accepted Solution

by:
knowlton earned 100 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: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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

932 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

15 Experts available now in Live!

Get 1:1 Help Now