Click here to Skip to main content
15,891,375 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
This is a windows application. Currently I print gif picture files like this:
C#
Image newImage = Image.FromFile("c:\\Pictures\\pic.gif");
Point ulCorner = new Point(300, 300);
e.Graphics.DrawImage(newImage, ulCorner);

Is there a way to incorporate this picture files in the program so I dont have to reference outside gif files?

Thank you for any help that can be provided.
Posted
Updated 9-Jun-12 23:56pm
v2

Is there a way to incorporate this picture files in the program so I dont have to reference outside gif files?
Yes you can. You can embed the image into your application resources and then use it as a source.

Embedded Resources: To add any file as an embedded resource in a windows forms application:
-> Right click your project name in solution explorer and click Add Existing item.
-> Select the file in the Solution Explorer, go to the Properties Window
-> Set its Build Action property to Embedded Resource.
-> File will be embedded within your compiled executable, when you build the project.

If needed, refer: Image Resources in WinForms and WPF[^]
 
Share this answer
 
v2
Comments
Al Yambo 10-Jun-12 7:33am    
Thank you. I did as you recommended. Very informative article. One follow up question-- Can this picture file be resized?
Sandeep Mewara 10-Jun-12 11:28am    
Resized as in display in your application? If so, depends on how you are using. You can set it to be displayed in a image control of a given size forcing it to fit in as set via properties.
Yes.
1) In the Solution Explorer pane, open the Properties branch of your project.
2) Double click "Resources.resx"
3) In the "Resources.resx" tab in you main window, you will see a toolbar - click on the drop down beside "Add Resource" and select "Add Existing File".
4) Browse to the image source, and open it. This will add the file to your resources. Save and close "Resources.resx".
5) Open the "Resources" branch of your project in the Solution Explorer pane. Highlight the new image, and look at the "Properties" pane.
6) Change the "Build Action" from "none" to "Embedded Resource"

You can now access the image via:
C#
myPictureBox.Image = Properties.Resources.MyImageName;
So if your file is called "PictureOfAnEagle.jpg":
C#
myPictureBox.Image = Properties.Resources.PictureOfAnEagle;
 
Share this answer
 
Comments
Al Yambo 10-Jun-12 7:18am    
Thank you. Exactly what I needed. I am now able to save the picture in the program and reference and print it as below. Now I have a place for all those picture files. My other question is-- Can these pictures be resized in the program (not that I need to at the moment)?

PictureBox newImage = new PictureBox();
newImage.Image = Properties.Resources.pic7;
Point ulCorner = new Point(300, 300);
e.Graphics.DrawImage(newImage.Image, ulCorner);
OriginalGriff 10-Jun-12 7:49am    
Yes.
Any image can be resized - there are several DrawImage overloads for it, including
http://msdn.microsoft.com/en-us/library/yws82c40.aspx
You don't need to store the Image in a Picture box - I only did that by way of illustration. You can use the Image directly:
e.Graphics.DrawImage(Properties.Resources.pic7, new Point (300,300));

There is even a Image method ready for making thumbnails if you need them:
http://msdn.microsoft.com/en-us/library/system.drawing.image.getthumbnailimage.aspx

Al Yambo 10-Jun-12 8:39am    
Thanks once again. The resize is working well. Glad to know too I can just use the image directly.
OriginalGriff 10-Jun-12 8:48am    
You're welcome!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900