Windows Scripting
  Home arrow Windows Scripting arrow Page 3 - Simplified Image Processing in GDI+
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Silverlight  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
ASP Web Hosting  
ASP.NET Web Hosting 
Windows Web Hosting
 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
WINDOWS SCRIPTING

Simplified Image Processing in GDI+
By: Xianzhong Zhu
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 1
    2008-09-10

    Table of Contents:
  • Simplified Image Processing in GDI+
  • Meta File Support in GDI
  • Sample One-Record and Replay Metafiles
  • Sample Two-Clipping and Scaling Images
  • Sample Three-Use Interpolation to Output
  • Sample Four-Flipping an Image

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Simplified Image Processing in GDI+ - Sample One-Record and Replay Metafiles


    (Page 3 of 6 )

    As is indicated above, to record a meta file is in fact to persist all the drawing instructions into a disk file. In this simple sample, you will learn how to save (record) the drawing instructions and replay them on the screen. Figure 1 shows the running-time snapshot.


    Figure 1-the running-time snapshot for sample 1


    The following indicates the related key code implementation:

    Graphics metagraph=this.CreateGraphics();

    //create a new meta file

    IntPtr hdc =metagraph.GetHdc();

    Metafile metaFile1 = new Metafile("SampMeta1.emf", hdc);

    //use the address of the Metafile object as the drawing plane

    Graphics graphics=Graphics.FromImage(metaFile1);


    //define a red to blue gradient brush

    LinearGradientBrush RtoBBrush=new LinearGradientBrush(

    new Point(0, 10),

    new Point(200, 10),

    Color.Red,

    Color.Blue);

    //define another blue to yellow gradient brush

    LinearGradientBrush BtoYBrush=new LinearGradientBrush(

    new Point(0, 10),

    new Point(200, 10),

    Color.Blue,

    Color.Yellow);

    Pen bluePen=new Pen(Color.Blue);

    //draw a ancient Chinese picture of the Eight Diagrams onto the 'screen'

    Rectangle ellipseRect=new Rectangle(0, 0, 200, 200);

    Rectangle left=new Rectangle(0, 50, 100, 100);

    graphics.DrawArc(bluePen,left,180.0f,180.0f);

    Rectangle right=new Rectangle(100, 50, 100, 100);

    graphics.FillPie(RtoBBrush, ellipseRect,0.0f,180.0f);

    graphics.FillPie(BtoYBrush, ellipseRect,180.0f,180.0f);

    graphics.FillPie(RtoBBrush, left,180.0f,180.0f);

    graphics.FillPie(BtoYBrush, right,0.0f,180.0f);


    //render some text

    SolidBrush solidBrush=new SolidBrush(Color.Black);

    FontFamily fontFamily=new FontFamily("MS Gothic");

    Font font=new Font(fontFamily, 27,

    FontStyle.Regular, GraphicsUnit.Pixel);

    graphics.DrawString("Meta File Test", font, solidBrush,

    new PointF(5.0f, 80.0f));

    //so far, GDI+ has merely store drawing instruction into the harddisk


    //release all the related resources used above

    graphics.Dispose();

    metaFile1.Dispose();

    metagraph.ReleaseHdc(hdc);

    metagraph.Dispose();


    //replay the drawing info created above

    Graphics playbackGraphics=this.CreateGraphics();

    playbackGraphics.Clear(Color.White);

    //open and render the previously-created meta file

    Metafile metaFile2 = new Metafile("SampMeta1.emf");

    playbackGraphics.DrawImage(metaFile2,new Point(0,0));

    //close the open meta file

    metaFile2.Dispose();

    There are two crucial steps in the previous sample. The first step is to create an empty meta file named "SampMeta1.emf,"draw images onto the image, and then render text in it. All of the drawing actions are done on a file in a hard disk-you can see nothing on the screen during the first step.

    The second step picks up from the last few lines. First create a Graphics object, then open the meta file created in the first step and finally, by using the DrawImage() method of the Graphics object, the true rendering plays on the screen. It is only then that you can visualize the rendering result, i.e. replay the meta file on the screen.

    One last word: after running the first sample, you may find a metafile named "SampMeta1.emf," which corresponds to the drawing instructions; without the second step to invoke the DrawImage() method you will not see anything on the screen.

    More Windows Scripting Articles
    More By Xianzhong Zhu


     

    WINDOWS SCRIPTING ARTICLES

    - More Windows Scripting Workarounds from Nilpo
    - Overloading Methods and More in VBScript
    - Improving MFC for Windows Vista
    - Regular Expressions in VBScript
    - Working with Dates in WMI
    - Completing Calendars with VBScript Date Func...
    - Building Calendars with VBScript Date Functi...
    - Working With Dates and Times in VBScript
    - Designing WCF DataContract Classes Using the...
    - Understanding Dates and Times in VBScript
    - Working With Arrays in VBScript
    - Compressed Folders in WSH
    - Using .NET Interops in VBScript
    - Nilpo`s Scripting Secrets, Vol I
    - Database operations using Silverlight 2.0 WC...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 6 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek