473,898 Members | 2,941 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Saving image in picbox

I used a picture box to draw lines and rectangle using its graphics
object in paint event. Now i need to save those lines i have drawn and
print them. I need to know how to save them. I tried image.save. But i
didn't work...
Mar 25 '08 #1
8 2250
Hi Vicky,

It is a common mistake that the PictureBox is meant for drawing, which it
isn't. It is meant to display the image inserted into the Image property, so
Image.Save would only save an existing image.

Anyhow, to save what you draw in the Paint event, extract the drawing code
to a separate method taking the Graphics object from PaintEventArgs as a
parameter.
When you want to save the drawing, simply create a Graphics object from a
blank Bitmap object and call the drawing method with this Graphics object as
a parameter.

Something like:

OnPaint(PaintEv entArgs e)
{
Draw(e.Graphics );
}

SaveImage()
{
Bitmap bmp = new Bitmap(100, 100);
using(Graphics g = Graphics.FromIm age(bmp))
{
Draw(g);
}
bmp.Save(filena me);
}

--
Happy Coding!
Morten Wennevik [C# MVP]
"vi*********@gm ail.com" wrote:
I used a picture box to draw lines and rectangle using its graphics
object in paint event. Now i need to save those lines i have drawn and
print them. I need to know how to save them. I tried image.save. But i
didn't work...
Mar 25 '08 #2
I tried as you said. It created a bmp file. But when i open it there
was no image. It was just an empty bmp file :( When i tried to open it
with paint it said format not supported :(

this is my code
public void drawwire(Graphi cs gr)
{
Pen p1 = new Pen(System.Draw ing.Color.Black , 6);
Color customColor = Color.Black;
SolidBrush b1 = new SolidBrush(cust omColor);
Point begin = new Point(), end = new Point();
for (int count1 = 1; count1 <= wire_cont; count1++)
{
if (modify.item == 1)
{
p1.Color = Color.Black;
b1.Color = Color.Black;

if (modify.no == count1)
{
p1.Color = Color.Red;
b1.Color = Color.Red;
}
}
if (wire[count1] != null)
{
begin = wire[count1].getBeginning() ;
end = wire[count1].getEnding();
gr.FillRectangl e(b1, begin.X - 4, begin.Y - 4, 8,
8);
gr.FillRectangl e(b1, end.X - 4, end.Y - 4, 8, 8);
gr.DrawLine(p1, begin, end);
}
}
}
private void toolStripButton 2_Click(object sender, EventArgs e)
{
Bitmap print = new Bitmap(100, 100);
using (Graphics pri = Graphics.FromIm age(print))
{
drawwire(pri);
}
print.Save("E:\ \print.bmp");
}
Mar 27 '08 #3
Hi Vicky,

Your code is correct as far as I can see, although I cannot reproduce your
code as the modify and wire data is unknown. The error would indicate you
are not producing a regular bitmap so if you are doing additional stuff to
the bitmap I would check that code.

Below is some reference code that should produce a 100x100 bitmap with a red
80x80 rectangle in the middle. This rectangle is also painted in a Panel
control using the exact same drawing code.

public Form1()
{
InitializeCompo nent();

panel1.Paint += new PaintEventHandl er(panel1_Paint );
}

void panel1_Paint(ob ject sender, PaintEventArgs e)
{
drawwire(e.Grap hics);
}

public void drawwire(Graphi cs gr)
{
gr.FillRectangl e(Brushes.Red, 10, 10, 80, 80);
}

private void button1_Click(o bject sender, EventArgs e)
{
Bitmap print = new Bitmap(100, 100);
using (Graphics pri = Graphics.FromIm age(print))
{
drawwire(pri);
}
print.Save("C:\ \print.bmp");
}

--
Happy Coding!
Morten Wennevik [C# MVP]
"vi*********@gm ail.com" wrote:
I tried as you said. It created a bmp file. But when i open it there
was no image. It was just an empty bmp file :( When i tried to open it
with paint it said format not supported :(

this is my code
public void drawwire(Graphi cs gr)
{
Pen p1 = new Pen(System.Draw ing.Color.Black , 6);
Color customColor = Color.Black;
SolidBrush b1 = new SolidBrush(cust omColor);
Point begin = new Point(), end = new Point();
for (int count1 = 1; count1 <= wire_cont; count1++)
{
if (modify.item == 1)
{
p1.Color = Color.Black;
b1.Color = Color.Black;

if (modify.no == count1)
{
p1.Color = Color.Red;
b1.Color = Color.Red;
}
}
if (wire[count1] != null)
{
begin = wire[count1].getBeginning() ;
end = wire[count1].getEnding();
gr.FillRectangl e(b1, begin.X - 4, begin.Y - 4, 8,
8);
gr.FillRectangl e(b1, end.X - 4, end.Y - 4, 8, 8);
gr.DrawLine(p1, begin, end);
}
}
}
private void toolStripButton 2_Click(object sender, EventArgs e)
{
Bitmap print = new Bitmap(100, 100);
using (Graphics pri = Graphics.FromIm age(print))
{
drawwire(pri);
}
print.Save("E:\ \print.bmp");
}
Mar 27 '08 #4
Hey it worked man :) :) :) Thanks a lot :) Do you know how to print
that picture? I'm using inkjet printer?????
Mar 31 '08 #5
I tried to open the BMP file using MSPAINT. But it's giving error that
"Paint cannot read this file. This is not a valid BMP file or this
format is not currently supported". What to do :(
Apr 1 '08 #6
You told me that this one will create a bmp file.
print.Save("C:\ \print.bmp",
ImageFormat.Bmp );

Ya it created one. But the content in pic box was nt
found. It just had a black background any nothig else :( :(

Apr 1 '08 #7
The printing worked :) thanks ya...
Apr 1 '08 #8
Wennevik i did as you told. But the resulting bitmap had only a black
background. Tell me how to overcome it ya
Apr 7 '08 #9

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

Similar topics

4
7522
by: Carl | last post by:
In the application I'm writing, I'm modifying a windows bitmap using the circle and line methods. Once the changes have been made, I would like to save the bitmap with the changes. My problem is, after saving the bitmap I find that the changes haven't been saved with the new file. Any help with what I'm doing wrong? -Carl
1
5154
by: halise irak via .NET 247 | last post by:
I get an "ArgumentException: Invalid parameter used at System.Drawing.Image.FromStream(Stream stream, Boolean useEmbeddedColorManagement)" exception. it is too ridicilious to get such an exception, can anyone see what might cause that exception? (By the way, i am trying to throw the header part of the image away and extract only
3
29948
by: Shailaja Kulkarni | last post by:
hi All, This problem is for C# windows application. I have a user control whcih contains one picture box along with other controls. Depending on certain conditions I want to change images on the picturebox. But at the same time I want to refer only assembly of the user control in other solution. If I am using method System.Drawing.Image.FromFile("filename") I have to put gif files containing those images in bin directory of solution in...
4
3313
by: dale zhang | last post by:
Hi, I am trying to save and read an image from MS Access DB based on the following article: http://www.vbdotnetheaven.com/Code/Sept2003/2175.asp Right now, I saved images without any errors. After reading the ole object from db, I saved it to C: as file1.bmp and displayed on the web. But it can not be displayed. After I manually sent the file to wordpad, it shows
2
3210
by: Crirus | last post by:
hello I have apicturebox painted in it's paint event. I need a way to save the image created. Image obj of my picturebox is Nothing. How to output to a jpg my image? Crirus
7
1689
by: Adam Maltby | last post by:
Hi, I am adding an new picbox and animated gif to it at runtime Dim img As Image = Image.FromFile(Application.StartupPath & "\animation.gif") With pic_Wait .Location = New Point(300, 250) .Width = 72 .Height = 101 .BackColor = Color.Transparent .BorderStyle = BorderStyle.None
3
3923
by: PaulJS | last post by:
Hi, I'm just getting started w/ VB.NET. I've been doing database development in MS Access, both as Access databases or as a front end for SQL Server. I need to create a VB.NET app to do some specific functions with SQL Server data. The main problem I'm having is trying to bind data from a SQL Server image data type field to a PictureBox control. For example, the image data field in my table will hold a JPG file of a scanned document...
3
4098
by: tedqn | last post by:
I'm using VS 2005. When user selects a image from the FileListBox, the code is PictureBox.ImageLocation = sFullPath PictureBox is set to AutoSize. When the newly loaded image is smaller than the panel size, I want to center it within the panel by calculating and move the box to a location other than (0,0). The
0
881
by: Bram2008 | last post by:
I have 2 picbox which contain lines. And i want to print it with "form.print" or "me.print". The problem is i can print the form, but the lines in picture box is gone or missing. ;). Please help me to print the form which contain picbox and its lines. NB: I use the plot line to draw a line in picbox. Thx.
0
9993
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
9841
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
10858
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...
1
10949
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10484
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
8036
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
7187
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
5882
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
4296
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.