Click here to Skip to main content
15,896,493 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, my question is simple, I scan images using WIA or5 TWAIN and I get the image in the form of IntPtr I want to insert this image in the database?
Posted

1 solution

You need to convert the unmanaged pointer to a managed array of bytes: Marshal.Copy[^] is what you need. But first, you need to find the size of the image buffer that the IntPtr points to, and allocate a big enough byte[]. Without your code, I can't tell you how to get that! :laugh:
 
Share this answer
 
Comments
Member 8491132 14-May-12 6:19am    
private void button1_Click(object sender, System.EventArgs e)
{
St_num = StudentPhoto.photo.fileName;
//SaveDIBAs( this.Text, bmpptr, pixptr ); want to use bmpptr
byte[] byte_Img;

//SqlConnection mySqlConn=new SqlConnection("Network Library=DBMSSOCN;"+"Data Source=82.201.143.224,2022;"+"Initial Catalog=oucu_card;"+"User ID=ousa;"+"Password=cO45_iu89");
SqlConnection mySqlConn = new SqlConnection("server=192.168.1.25;workstation id=localhost;packet size=4096; persist security info=False;user id=araslan;Password=hR@oucu1;Initial catalog=mandoobeen;min pool size=1;max pool size=10000000;connection lifetime=6000");


SqlCommand sqlcmd = new SqlCommand();
sqlcmd.Connection=mySqlConn;
sqlcmd.CommandText="insert into " + StudentPhoto.photo.tableName + " (nationalNumber,pictureName,Picture) values(@nN,@pN,@pic)";

sqlcmd.Parameters.Add("@nN",System.Data.SqlDbType.NVarChar);
sqlcmd.Parameters.Add("@pic",System.Data.SqlDbType.Image);
sqlcmd.Parameters.Add("@pN",System.Data.SqlDbType.NVarChar);


FileStream fs=new FileStream(this.textBox2.Text.ToString(),FileMode.Open,FileAccess.Read,FileShare.Read);
byte_Img=new byte[Convert.ToInt32(fs.Length)];
int iBytesRead=fs.Read(byte_Img,0,Convert.ToInt32(fs.Length));
fs.Close();

sqlcmd.Parameters["@nN"].Value = StudentPhoto.photo.nationalNumber;
sqlcmd.Parameters["@pic"].Value=byte_Img;
TwainGui.Login l = new TwainGui.Login();
sqlcmd.Parameters["@pN"].Value = StudentPhoto.photo.fileName;

mySqlConn.Open();
int iresult=sqlcmd.ExecuteNonQuery();
mySqlConn.Close();


System.Diagnostics.Process process1;
process1= new System.Diagnostics.Process();
process1.EnableRaisingEvents = false;
Directory.SetCurrentDirectory(textBox3.Text);
Application.Exit();
}
OriginalGriff 14-May-12 6:45am    
That code doesn't help - it doesn't show where you get your image data into bmpptr from - you need to look at that, and work out what the length is - it will tell you somewhere because you need it to do any image manipulations!
Member 8491132 14-May-12 7:14am    
mainframe.cs
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using TwainLib;

namespace TwainGui
{
public class MainFrame : System.Windows.Forms.Form, IMessageFilter
{
private System.Windows.Forms.MenuItem menuMainFile;
private System.Windows.Forms.MenuItem menuItemScan;
private System.Windows.Forms.MenuItem menuItemSelSrc;
private System.Windows.Forms.MenuItem menuItemExit;
private System.Windows.Forms.MenuItem menuItemSepr;
private System.Windows.Forms.MainMenu mainFrameMenu;
private System.Windows.Forms.Button button1;
private IContainer components;
// public int OperatorId;
// public MainFrame(/*int OperatorId*/)
// {
//// this.OperatorId=OperatorId;
// //this.OperatorExamPlaceNumber=OperatorExamPlaceNumber2;
//
// //
// // Required for Windows Form Designer support
// //
// InitializeComponent();
//
// //
// // TODO: Add any constructor code after InitializeComponent call
// //
// }


public MainFrame()
{
InitializeComponent();
tw = new Twain();
tw.Init( this.Handle );
}

protected override void Dispose( bool disposing )
{
if( disposing )
{
tw.Finish();
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
///
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
///

private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(MainFrame));
this.menuMainFile = new System.Windows.Forms.MenuItem();
this.menuItemSelSrc = new System.Windows.Forms.MenuItem();
this.menuItemScan = new System.Windows.Forms.MenuItem();
this.menuItemSepr = new System.Windows.Forms.MenuItem();
this.menuItemExit = new System.Windows.Forms.MenuItem();
this.mainFrameMenu = new System.Windows.Forms.MainMenu(this.components);
this.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// menuMainFile
//
this.menuMainFile.Index = 0;
this.menuMainFile.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuItemSelSrc,
this.menuItemScan,
this.menuItemSepr,
this.menuItemExit});
this.menuMainFile.MergeType = System.Windows.Forms.MenuMerge.MergeItems;
this.menuMainFile.Text = "&File";
//
// menuItemSelSrc
//
this.menuItemSelSrc.Index = 0;
this.menuItemSelSrc.MergeOrder = 11;
this.menuItemSelSrc.Text = "&Select Source...";
//
// menuItemScan
//
this.menuItemScan.Index = 1;
this.menuItemScan.MergeOrder = 12;
this.menuItemScan.Text = "&Acquire...";
this.menuItemScan.Click += new System.EventHandler(this.menuItemScan_Click);
//
// menuItemSepr
//
this.menuItemSepr.Index = 2;
this.menuItemSepr.MergeOrder = 19;
this.menuItemSepr.Text = "-";
//
// menuItemExit
//
this.menuItemExit.Index = 3;
this.menuItemExit.MergeOrder = 21;
this.menuItemExit.Text = "&Exit";
//
// mainFrameMenu
//
this.mainFrameMenu.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuMainFile});
//
// button1
//
this.button1.Anchor = System.Windows.Forms.AnchorStyles.None;
this.button1.BackColor = System.Drawing.SystemColors.ActiveCaptionText;
this.button1.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.button1.Font = new System.Drawing.Font("Microsoft Sans Serif", 15.75F, System.Drawing.FontStyle.Regular, Syst
Member 8491132 14-May-12 7:15am    
picform.cs
using System;
using System.Text;
using System.Runtime.InteropServices;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using GdiPlusLib;
using System.Drawing.Imaging;
using OucuFunctions ;
using System.Security.Permissions;
using System.Threading;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
using System.Net.Sockets;
using FTP_DLL;
using OpenEducationCenter.common_classes;
using System.Net;
using Rebex.Net;
using Rebex.Samples;

namespace TwainGui
{
public class PicForm : System.Windows.Forms.Form
{
private System.ComponentModel.Container components = null;
//string strCmdLine;
public string St_num;
private System.Windows.Forms.TextBox textBox2;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.TextBox stNTxt;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.TextBox textBox3;
// public int OperatorId;



// public PicForm(/*int OperatorId*/)
// {
//// this.OperatorId=OperatorId;
// //this.OperatorExamPlaceNumber=OperatorExamPlaceNumber2;
//
// //
// // Required for Windows Form Designer support
// //
// InitializeComponent();
//
// //
// // TODO: Add any constructor code after InitializeComponent call
// //
// }
//
protected static string sst = "";
public PicForm( IntPtr dibhandp )
{

InitializeComponent();
SetStyle( ControlStyles.DoubleBuffer, false );
SetStyle( ControlStyles.AllPaintingInWmPaint, true );
SetStyle( ControlStyles.Opaque, true );
SetStyle( ControlStyles.ResizeRedraw, true );
SetStyle( ControlStyles.UserPaint, true );
bmprect = new Rectangle( 0, 0, 0, 0 );
dibhand = dibhandp;
bmpptr = GlobalLock( dibhand );
pixptr = GetPixelInfo( bmpptr );
this.AutoScrollMinSize = new System.Drawing.Size( bmprect.Width, bmprect.Height );
}

protected override void Dispose( bool disposing )
{
if( disposing )
{
if( dibhand != IntPtr.Zero )
{
GlobalFree( dibhand );
dibhand = IntPtr.Zero;
}

if(components != null)
{
components.Dispose();
}
}

base.Dispose( disposing );
}

#region Windows Form Designer generated code
///
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
///

private void InitializeComponent()
{
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(PicForm));
this.textBox2 = new System.Windows.Forms.TextBox();
this.textBox1 = new System.Windows.Forms.TextBox();
this.button1 = new System.Windows.Forms.Button();
this.stNTxt = new System.Windows.Forms.TextBox();
this.button2 = new System.Windows.Forms.Button();
this.textBox3 = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// textBox2
//
this.textBox2.Location = new System.Drawing.Point(16, 147);
this.textBox2.Name = "textBox2";
this.textBox2.Size = new System.Drawing.Size(144, 20);
this.textBox2.TabIndex = 17;
this.textBox2.Text = "textBox2";
this.textBox2.Visible = false;
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(216, 392);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(240, 20);
this.textBox1.TabIndex = 19;
this.textBox1.Text = "ftp:/Studentimages:1234@1234@192.168.1.25:990";
this.textBox1.Visible = false;
//
// button1
//
this.button1.BackColor = System.Drawing.Color.White;
this.button1.FlatStyle = System.W
OriginalGriff 14-May-12 9:30am    
No, I'm not going to wade through your entire program! :laugh:
Have a look at it yourself - it will tell you somewhere.

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