Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Metafile problem

Posted on 2004-08-15
12
Medium Priority
?
2,062 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

Tech or Treat! - Giveaway

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

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

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

610 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