Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8971053
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T17:51:42+00:00 2026-06-15T17:51:42+00:00

Possible Duplicate: Print Windows form in c# I need to print the form where

  • 0

Possible Duplicate:
Print Windows form in c#

I need to print the form where the print button is on:

private void btnPrint_Click(object sender, EventArgs e)
{
    Graphics g1 = this.CreateGraphics();
    Image MyImage = new Bitmap(this.ClientRectangle.Width, this.ClientRectangle.Height, g1);
    Graphics g2 = Graphics.FromImage(MyImage);
    IntPtr dc1 = g1.GetHdc();
    IntPtr dc2 = g2.GetHdc();
    BitBlt(dc2, 0, 0, this.ClientRectangle.Width, this.ClientRectangle.Height, dc1, 0, 0, 13369376);
    g1.ReleaseHdc(dc1);
    g2.ReleaseHdc(dc2);
    MyImage.Save(@"c:\PrintPage.jpg", ImageFormat.Jpeg);
    FileStream fileStream = new FileStream(@"c:\PrintPage.jpg", FileMode.Open, FileAccess.Read);
    StartPrint(fileStream, "Image");
    fileStream.Close();
    if (System.IO.File.Exists(@"c:\PrintPage.jpg"))
    {
        System.IO.File.Delete(@"c:\PrintPage.jpg");
    }
}

But is gives me an error at: MyImage.Save.

The error is: ExternalException was Unhandled: A generic error occurred in GDI+.

Can someone give me a fix for this problem,and explain, why I’m getting this error?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T17:51:43+00:00Added an answer on June 15, 2026 at 5:51 pm

    The exception message is crappy but indicates that GDI+ cannot write the file.

    There are at least two problems. First off, a program is not allowed to write to c:\ without having admin privileges on Vista, Win7 and Win8 obtained through a UAC prompt. Or any non-admin account on previous Windows versions. Use Path.GetTempFileName() instead.

    Second one is that you are sloppy about disposing MyImage. That will very likely make your program fail the second time you run this code since the file is still in use and the garbage collector didn’t get around to finalize the image object yet. Image.Dispose() is required to release the lock on the image file. Use the using statement to ensure it is consistently disposed.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: TSQL How do you output PRINT in a user defined function? This
Possible Duplicate: How to print out the method name and line number and conditionally
Possible Duplicate: Why do I see strange values when I print uninitialized variables? Fun
Possible Duplicate: Is there a clean way to prevent windows.h from creating a near
Possible Duplicate: How to print in Python without newline or space? How to print
Possible Duplicate: Is it possible to print out the size of a C++ class
Possible Duplicate: Filetype association with application (C#) I'm writing a C# Windows app to
Possible Duplicate: Changing the width of a Windows console window? Im writing a simple
Possible Duplicate: backspace character weirdness I have noticed that 1. If I print only
Possible Duplicate: How are echo and print different in PHP? UPDATE : I found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.