473,385 Members | 2,015 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

How to Save and Retrive image?

Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using System.IO;
  10. using System.Drawing.Imaging;
  11.  
  12. namespace WindowsFormsApplication1
  13. {
  14.     public partial class Form1 : Form
  15.     {
  16.         public Form1()
  17.         {
  18.             InitializeComponent();
  19.         }
  20.  
  21.         private void button1_Click(object sender, EventArgs e)
  22.         {
  23.             MemoryStream stream = new MemoryStream();
  24.             pictureBox1.Image.Save(stream, ImageFormat.Jpeg);
  25.             byte[] memImageData = stream.ToArray();
  26.  
  27.         }
  28.  
  29.         private void button2_Click(object sender, EventArgs e)
  30.         {
  31. OpenFileDialog OpenFileMemPhoto = new OpenFileDialog();
  32. OpenFileMemPhoto.Title = "Open File";
  33. OpenFileMemPhoto.Filter = "All files (*.*)|*.*|All files (*.*)|*.*";
  34. OpenFileMemPhoto.FilterIndex = 2;
  35. OpenFileMemPhoto.RestoreDirectory = true;
  36. if (OpenFileMemPhoto.ShowDialog() == DialogResult.OK && OpenFileMemPhoto.FileName != null)
  37.             {
  38. txt_mem_image_address.Text = OpenFileMemPhoto.FileName;
  39.                 pictureBox1.Load(OpenFileMemPhoto.FileName);
  40.             }
  41.         }
  42.     }
  43. }
  44.  


The error is:

A generic error occurred in GDI+.


Please give me solutions if any.......
Jul 4 '11 #1
3 1858
GaryTexmo
1,501 Expert 1GB
Can you please be a little more descriptive about where the problem occurs? Does it happen in button1's click event handler, or button2's?

I've seen this error before (though under slightly different circumstances) and I know the exception gets thrown at the application level. When I saw it the cause was closing a memory stream that I had loaded an image from. This case looks somewhat different but lets be sure of what's going on before I try to start helping you out :)
Jul 4 '11 #2
I have used the following line and problem solved.

pictureBox1.Image = new Bitmap(OpenFileMemPhoto.FileName);


thank you for your reply
Jul 5 '11 #3
GaryTexmo
1,501 Expert 1GB
Keep in mind that when you do that, an open file handle is left on the file... this isn't always undesirable, just something to be aware of.

(Loading an image into a memory stream to make a copy is a way to work around this, which is what I thought you were doing :D)
Jul 5 '11 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: thomaz | last post by:
I would like to know the correct way to save an image showing in a Picture Box???? Thanks....
1
by: Smita Kashyap | last post by:
Hello to all, Which one is flexible/optimal solution to save uplaoded image. Frist save image at server or from where you're site pages are save in any image folder and save only image name or...
4
by: =?iso-8859-1?B?RulybmFz?= | last post by:
Hello all, How can I save a image that is on internet (eg: http://www.agalera.com.br/_img/layout/logo.gif)??? I would like to save the image on my server... many thanks
0
by: ramya naidu | last post by:
Hai friends i have cod eto draw and save picture in textbox and i need to save that image so iam using this code when i save the image and reopen it is not saving plz any one help me i have written...
1
by: kiekar | last post by:
Hello, A yes or no answer would be much appreciated. I have a word document which has a sqlxml annotated schema mapped to it. On the word document there is a image inserted to it. Is it possible...
1
by: amritranjan | last post by:
How to retrive image file from MS access database and display this in another JSPpage -------------------------------------------------------------------------------- This is my Jsp code for...
0
by: admiralato | last post by:
Hi guys, I need to insert an image to an email. The IMAGE.SRC of the image is from a file named, Graphics.aspx. What I'm thinking is in order for me to include the generated image in the email,...
2
by: chinnaits | last post by:
how to retrive image from the database
6
by: jeddiki | last post by:
Hi, I am using snapcasa.com to provide me with an image. They encourage you to cache the images on your own server so I am trying to do that. When I use this code to display the image it...
0
by: sahil khan | last post by:
how i can use image list control in vb.net, and how to save jpeg image with customer record and i wnat to also save this data base ms access.
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...

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.