473,714 Members | 2,500 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to save pictureBox to a file ?

Hello
I tried:
pictureBox1.Ima ge.Save("file.s av");

compiled, but when i run i receive errors:
Additional information: Object reference not set to an instance of an
object.

Why ?

Thanx
Michal

Nov 16 '05 #1
7 25492
What you want to do is instead of drawing on the pictureBox you want to create
an Image, draw invisibly on that, then put the Image on the pictureBox.
pictureBox1.Ima ge = new Bitmap(pictureB ox1.Width, pictureBox1.Hei ght);
Image bmp = pictureBox1.Ima ge;
Graphics g = Graphics.FromIm age(bmp);

//Edit bmp...any drawing goes here using 'g'
g.FillRectangle (new SolidBrush(Colo r.White), 0, 0, pictureBox1.Wid th,
pictureBox1.Hei ght);
for(int I=0; I < pictureBox1.Wid th; I+)
g.DrawLine(new Pen(Color.Black ), pictureBox1.Wid th - I, 0, 0, I);

//Display bmp
pictureBox1.Ima ge = bmp;

then to save you could save the pictureBox.Imag e or you could make bmp public
and save that directly either would work, the second probably would be better.

Hope that answers your question.
On 15/04/2004 us**@domain.inv alid wrote:
Hello
I tried:
pictureBox1.Im age.Save("file. sav");

compiled, but when i run i receive errors:
Additional information: Object reference not set to an instance of an
object.

Why ?

Thanx
Michal

Nov 16 '05 #2
This piece of code works fine for me either:

// Code:
private void buttonSave_Clic k(object sender, System.EventArg s e)
{
SaveFileDialog save = new SaveFileDialog( );
save.Filter = "Bitmap files (*.bmp)|*.bmp|J PG files (*.jpg)|*.jpg|G IF
files (*.gif)|*.gif|A ll files (*.*)|*.*";
save.FilterInde x = 4;
save.RestoreDir ectory = true;

if(save.ShowDia log() == DialogResult.OK )
{
myPictureBox.Im age.Save(save.F ileName);
}
}
Nov 16 '05 #3
Hello

That's strange, i have identical code and still the same error:

SaveFileDialog save = new SaveFileDialog( );
save.Filter = "Bitmap files (*.bmp)|*.bmp|J PG files (*.jpg)|*.jpg|G IF
files (*.gif)|*.gif|A ll files (*.*)|*.*";
save.FilterInde x = 4;
save.RestoreDir ectory = true;
if(save.ShowDia log() == DialogResult.OK )
{/*LINE 100: HERE IS THE PROBLEM*/
pictureBox1.Ima ge.Save(save.Fi leName);
}

when i check in line 100 i receive that pictureBox1.Ima ge==null,
but how is it possible ?? Durring saving i see my paintings
on that pictureBox.

Thanx
Michal

Nov 16 '05 #4


i found that only the second method described by Scatropolis works for
me fine. I wonder why...

Nov 16 '05 #5
hmm for some reason it didn't paste correctly.....

On 15/04/2004 "Scatropoli s" <ch*****@frayed .net> wrote:
What you want to do is instead of drawing on the pictureBox you want to createan Image, draw invisibly on that, then put the Image on the pictureBox.
pictureBox1.Im age = new Bitmap(pictureB ox1.Width, pictureBox1.Hei ght);
Image bmp ictureBox1.Imag e;
Graphics g raphics.FromIma ge(bmp);

//Edit bmp...any drawing goes here using 'g'
g.FillRectangl e(new SolidBrush(Colo r.White), 0, 0, pictureBox1.Wid th,
pictureBox1.He ight);
for(int I I < pictureBox1.Wid th; I+)
g.DrawLine(new Pen(Color.Black ), pictureBox1.Wid th - I, 0, 0, I);

//Display bmp
pictureBox1.Im age mp;


this line should be

picturebox1.Ima ge = bmp;
Nov 16 '05 #6
Ack it seems when I pasted it letters got deleted. trying to change everything

pictureBox1.Ima ge = new Bitmap(pictureB ox1.Width, pictureBox1.Hei ght);
Image bmp = PictureBox1.Ima ge;
Graphics g = Graphics.FromIm age(bmp);

//Edit bmp...any drawing goes here using 'g'
g.FillRectangle (new SolidBrush(Colo r.White), 0, 0, pictureBox1.Wid th,
pictureBox1.Hei ght);
for(int I I < pictureBox1.Wid th; I+)
g.DrawLine(new Pen(Color.Black ), pictureBox1.Wid th - I, 0, 0, I);

//Display bmp
pictureBox1.Ima ge = bmp;

there....that's kinda weird, sorry
Nov 16 '05 #7
When you draw on a pictureBox it is not saved into the image. I don't think
it's even saved anywhere. Which is why when it gets minimized or covered over
it's erased.

On 15/04/2004 us**@domain.inv alid wrote:


i found that only the second method described by Scatropolis works for
me fine. I wonder why...

Nov 16 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
11262
by: Maya | last post by:
Hey, there! I'm new to vb.net and it seems I wouldn't be able to solve this without help. I have a pipe delimited file that has to be saved in txt format so it could be accepted by my DTS package. If I open and save the file manualy in Wordpad - it works. (not in Notepad otherwise formatting is changed and DTS package won't accept it.)
0
14955
by: JTMar | last post by:
I need to save a file in UTF-8 format from an ASP file. Using FileSystemObject I can only save them in Unicode (UTF-16, I think) but not in UTF-8: Set zzzz = fso.OpenTextFile(filepath, 2, true, -1) How do I can save my file in UTF-8 format ? I have tried to convert the file to UTF-8, with this code:
0
6435
by: a | last post by:
Save text file as html kloepper 17:42 23 Jul '04 I'm using httpwebresponse and a StringBuilder to return a stream that originates as a file with the .txt suffix (My download code converts the html tags in this text file to well formed XHTML tags). The contents of the file contain html tags and the data in those tags. eg., <table cellSpacing=0 cellPadding=0 width="100%" align=center border=0> <TBODY> <tr> <td width="48%"></td> <td...
2
2364
by: Alvo von Cossel I | last post by:
hi, i have a textbox in a form. when you press a Save button, a savefiledialog appears. creating ther file works but everytime you click save, the dialog appears. how do i make the dialog appear once and then only save the file? thanx -- Alvo von Cossel I of Germany
8
2698
by: DanB | last post by:
This is probably soooo simple but I can't seem to get it. I have a text file that I want users to download via a web page. I want the file to be saved to a default folder (or one that they choose) on the users / client file system. I have toyed with a Self-Extracting zip file but the contents of my zip file changes each time it is downloaded so that invalidates the exe file. Also the text file is so small it is a waste to zip it.
5
3499
by: Patrick | last post by:
Following on from the excellent example at http://www.c-sharpcorner.com/Code/2003/Sept/ExportASPNetDataGridToExcel.asp on how to save a data-grid to excel file, how can I extend the example such that when the user click on the button a popup appear in IE to say "Do you want to save or open file?"
1
2194
by: Jennyfer Barco | last post by:
A Web Application Hello I have a .NET project, a Web Application and I need to save a file with some data the user selects. If I use the command FileOpen it will save the file in the server and not in the local machine where I'm running the application. Is there a way to save a file in the local machine and not the web server. I use the function GetFolders and brings me the directories in the path but for the server machine, not the...
4
2574
by: Matt | last post by:
Hi, I would like to save a file locally (on the client computer) in an ASP.net application. It is like the server would return some data that the user can save to a file on his local PC. The ASP page has a text field to type in the file path directly and a "Browse..." button, that shall display a "Save as..." dialog. I tried with the FileUpload control, but this does not work, since I do not want to upload an existing file, but I want to...
3
2204
by: jaikant | last post by:
Hi, I am loading a xml file using javascript in Pocket Internet Explorer for local HTML file. I am not able to save the xml file back to the PPC device. foodXML = new ActiveXObject("Msxml2.DOMDocument"); newElement = foodXML.createElement(textstr); foodXML.appendChild(newElement); foodXML.save("test.xml"); Please help me to save xml file to local folder using javascript. Thanks
4
3299
by: Busbait | last post by:
Hi, The below code will allow the user to open the Save As dialog box, Now my question is : after the user specify the file name and the path, how can I modify the below code in order to save the file in Excel format when the user click on the Save button What I need is to use the file name which was specified by the user in the dialog box , and then create an Excel file using the same name & path and the new excel file should...
0
8801
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8707
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9314
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9174
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
7953
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6634
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5947
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4464
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
2520
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.