473,513 Members | 3,569 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Saving image as OLE Object

Sl1ver
196 New Member
i've got this piece of coding. i need help with opening a picture box and then saving it as OLE Object in access DB

How would i massage this code tho fit in?

Expand|Select|Wrap|Line Numbers
  1. try
  2. {
  3.    //save image to byte array and allocate enough memory for the image
  4.    byte[] imagedata = image.ToByteArray(new Atalasoft.Imaging.Codec.JpegEncoder(75));
  5.  
  6.    //create the SQL statement to add the image data
  7.    myConnection = new OleDbConnection(CONNECTION_STRING);
  8.    OleDbCommand myCommand = new OleDbCommand("INSERT INTO Atalasoft_Image_Database (Caption, ImageData) VALUES ('" + txtCaption.Text + "', ?)", myConnection);
  9.    OleDbParameter myParameter = new OleDbParameter("@Image", OleDbType.LongVarBinary, imagedata.Length);
  10.    myParameter.Value = imagedata;
  11.    myCommand.Parameters.Add(myParameter);
  12.  
  13.    //open the connection and execture the statement
  14.    myConnection.Open();
  15.    myCommand.ExecuteNonQuery();
  16. }
  17. finally
  18. {
  19.    myConnection.Close();
  20. }
  21.  
Here is my open file code (works fine)
Expand|Select|Wrap|Line Numbers
  1. public void openBMP(String filename)
  2.         {
  3.             //Declare the OpenFileDialog as a new OpenFileDialog.
  4.             OpenFileDialog openFileDialog = new OpenFileDialog();
  5.             //Declare the PictureBox as a new PictureBox
  6.             //PictureBox pic = new PictureBox();
  7.             //Set the openFileDialog's Filter.
  8.             openFileDialog.Filter = "Bitmap Image|*.bmp|All Files|*.*";
  9.             //Set the openFileDialog's FileName to nothing.
  10.             openFileDialog.FileName = "";
  11.             string path = "";
  12.             //Show the openFileDialog
  13.             if (openFileDialog.ShowDialog(this) == DialogResult.OK)
  14.             {
  15.                 //To do code if the Open button was pressed
  16.                 //Use something like:
  17.                 filename = openFileDialog.FileName;
  18.                 pic.Image = Image.FromFile(filename);
  19.                 pic.Size = new System.Drawing.Size(280, 140);
  20.                 pic.SizeMode = PictureBoxSizeMode.CenterImage;
  21.                 path = System.IO.Path.GetDirectoryName(openFileDialog.FileName) + "\\" + openFileDialog.FileName;
  22.                 FullFileName = openFileDialog.FileName; 
  23.                 txtComp_Image.Text = path;
  24.  
  25.  
  26.             }
  27.             else
  28.             {
  29.                 return;
  30.             }
  31.  
and here is my insert code

Expand|Select|Wrap|Line Numbers
  1.  string UpdateStr = "";
  2.             if(count == 0)
  3.             {
  4.  
  5.  
  6.                 int i = 1;
  7.                 UpdateStr = "Insert into qx_Company " +
  8.                                           "(AST__ID, AST__NAME, AST__STREET, AST__TOWN, AST__CITY, AST__TEL, AST__FAX, AST__MAIL, AST__SITE, AST__POST, AST__PATH )" +
  9.                                           "values('" + i.ToString() + "','" + txtComp_Name.Text + "','" + txtComp_Street.Text + "','" + txtComp_Town.Text + "','" + txtComp_City.Text + "','" + txtComp_Tel.Text + "','" + txtComp_Fax.Text + "','" + txtComp_Email.Text + "','" + txtComp_Website.Text + "','" + txtComp_Box.Text + "','" + txtComp_Image.
  10.  
as i said, i just want to save the picture as a OLE Object
Help is much appreciated.
May 14 '09 #1
1 11576
Curtis Rutland
3,256 Recognized Expert Specialist
Notice how in the first piece of code that they use parameters instead of long string concatenation?
Expand|Select|Wrap|Line Numbers
  1. OleDbCommand myCommand = new OleDbCommand("INSERT INTO Atalasoft_Image_Database (Caption, ImageData) VALUES ('" + txtCaption.Text + "', ?)", myConnection);
  2. OleDbParameter myParameter = new OleDbParameter("@Image", OleDbType.LongVarBinary, imagedata.Length);
  3. myParameter.Value = imagedata;
  4. myCommand.Parameters.Add(myParameter);
When this code is executed, the OleDbCommand will replace the "?" with the parameter's value...which in this case is a byte[].

In the open file part of your code, you should use a System.IO.FileStream to read the file's contents into a byte[] and use that for the parameter to insert into the DB.

Make sure you always close any Streams you open.
May 14 '09 #2

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

Similar topics

2
15664
by: Sergiusz Michalski | last post by:
Hallo!! According to my previous post with popups hanging, now I'm trying to bypass this strange behaviour using loading JavaScript image not by image.src=URL but straight away from stream using HTTP GET method. The problem is, how to assign stream from downloaded image (ex. GIF89a) to a JavaScript Image object ? Below is my code with...
1
6884
by: Dev | last post by:
Dear Friends, I am passing the image name, size (in bytes) and imgaeformat (like jpg or bmp or pdf) through the network. I want display the image into picturebox without saving image files into Hard disk. Just display the image into PictureBox.... Is it Possible? If so how do to this?
0
1893
by: Deepak C.G via .NET 247 | last post by:
I want to dispose the image object in my child form, unless I won't dispose this object i can't delete the image file in my folder. I get this error in MDIparent form "An unhandled exception of type 'System.ArgumentException' occurred in system.windows.forms.dll" "Additional information: Invalid parameter used." Here is my code:-
2
6425
by: Martin Honnen | last post by:
I was playing around with canvas support in recent Safari, Mozilla and Opera (only version 9 preview) but run into issues with Safari related to the very old DOM Level 0 Image object for preloading images. When doing e.g. var img = new Image(); img.onload = function (evt) { alert(this); }; img.src = 'whatever.gif';
0
1226
by: Jianwei Sun | last post by:
I had a question on how to bind an image object to the ObjectDataSource. Basically, I use an ObjectDataSource to insert a record to sql server. I want to include an image object as a parameter in the record I want to insert. I want to find right way to construct the parameter object. Something like that:
1
2802
by: Rob T | last post by:
I made an image slideshow with precaching that uses three overlapping div tags that each contain an img tag that can be accessed using getElementById(). If imgObj is my image object, I change imgObj.src to the image URL and I'm checking imgObj.complete to see if it has been loaded. The problem is that when I change an imgObj that previously...
3
1593
by: Brian | last post by:
I have a windows forms project done in vb.net. One of the forms creates graphs based on the data supplied to it. I want to take multiple graphs and paste them into a larger Image object. I already have the code to create a single graph. I'm hoping that I can take these graphs and paste them into a given spot in the larger image. I've...
3
1221
by: luigidavinci | last post by:
Hi there, I just found these forums and several questions I had have been answered by just reading the posts but this one. I have simplified my code in order to just get to the point with my question. This is my html code: <html> <head> </head> <body>
0
1456
by: norseman | last post by:
Daniel de Sousa Barros wrote: ================================================ Click Start/settings/control panel/system/advanced/settings(in \ performance area)/advanced/change set System managed size on click SET box to right of above line click positives (OK APPLY etc.) all way out, system will probably reboot
0
7270
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...
0
7178
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...
0
7563
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...
1
7125
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...
0
4757
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...
0
3252
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...
0
3239
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1612
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
470
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.