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
Solved

Metafile problem

Posted on 2004-08-15
12
2,013 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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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
 
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying 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

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
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.
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

837 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