Depressed for a long time, it turned out to be this question: About C # About the picture to save to the database and display th

xiaoxiao2021-03-05  52

Ability to save pictures to the database, but display always errors, prompt: invalid parameters. Depressed for a long time, I found a lot of information on 9CBS but still can't. Later, I saw the content in the database, I found that it is not the length, because I save the picture when I save the picture, the length of Length is very large (hundred thousand or even bigger), but from the Byte [] Length of the database only 13. Later, I finally changed the length of the stored length. There is no white fee. I have been working for hours.

But fortunately, I finished over more than an hour before the Champion Cup, just seeing the game of Bayern and Chelsea (the first round of cutting 4: 2).

Attachment: code

private System.Windows.Forms.PictureBox pictureBox1; private System.Data.SqlClient.SqlCommand cmdExecute; private System.Data.SqlClient.SqlConnection connEmployee; private Bitmap image; private byte [] buffer;

Private void showmyImage (string filetodisplay, int xsize, int ysize) {// sets up an image object to be displayed. if (image! = null) {image.dispose ();

. // Stretches the image to fit the pictureBox pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage; image = new Bitmap (fileToDisplay); pictureBox1.ClientSize = new Size (xSize, ySize); pictureBox1.Image = (Image) image;}

private void button1_Click (object sender, System.EventArgs e) {OpenFileDialog openFileDialog = new OpenFileDialog (); openFileDialog.Title = "select a photo"; openFileDialog.ReadOnlyChecked = true; openFileDialog.ShowReadOnly = true; openFileDialog.Filter = "photos Document | *. * | Jpeg; *. Jpee; *. Jfif) | * .jpg; *. Jpeg; *. Jpee; *. Jfif.ji file (* .bmp) | * .Bmp | gif (*. GIF) | * .gif ";

String PictureName = ""; if (OpenFileDialog.showdialog () == DialogResult.ok) {PictureName = OpenFiledialog.FileName; this.ShowMyimage (PictureName, 112, 130);}

if (pictureName = ""!) {FileStream stream = new FileStream (pictureName, FileMode.Open, FileAccess.Read); buffer = new byte [stream.Length]; stream.Read (buffer, 0, (int) stream.Length ); Stream.close ();} this.cmdexecute.commandtext = @ "Insert INTO Photo_TEST (PHOTO) VALUES (@Photo)"; this.cmdexecute.Parameters.add ("@photo", sqldbtype.image, buffer.length , "Photo"); this.cmdexecute.Parameters ["@photo"]. Value = buffer; if (this.connemploye.state == connectionState.closed) this.connemploye.Open (); this.cmdexecute.executenonury () }

private void button2_Click (object sender, System.EventArgs e) {SqlDataAdapter da = new SqlDataAdapter ( "Select * from Photo_Test WHERE ID = 20", this.connEmployee); DataSet ds = new DataSet (); da.Fill (ds); DataTable DT = DS.TABLES [0]; Byte [] b = (byte []) Dt.Rows [0] [1];

IF (B.Length> 0) {MemoryStream Stream = New MemoryStream (B, True); Stream.Write (B, 0, B.Length); Image = New Bitmap; Stream.Close (); this.PictureBox1 }}}

转载请注明原文地址:https://www.9cbs.com/read-36628.html

New Post(0)