本文介绍了从MySQL撷取图片时发生错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在获取参数无效错误..我试图从C#中的MySQL数据库中检索图像,请帮助我..

I am getting parameter is not valid error..i tried to retrieve image from the MySQL database in C# please help me..

MySqlConnection myCon = new MySqlConnection(strProvider);
string q1 = "SELECT imgfile from myimages WHERE ImageID='" +imageid+ "'";

MySqlCommand cmd = new MySqlCommand(q1, myCon);
myCon.Open();
      
byte[] img = (byte[])cmd.ExecuteScalar();

MemoryStream str = new MemoryStream(img,true);
str.Write(img, 0, img.Length);
       
Bitmap bit = (Bitmap)Bitmap.FromStream(str,true);//getting error at this point
Response.ContentType = "image/Jpeg"
bit.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
myCon.Close();



非常感谢Sandeep Mewara ....但是现在我得到的是黑色背景而不是图像..我可以知道为什么的原因吗??
有人请帮助我..关于这个问题......



Thanks a lot Sandeep Mewara.... but now i am getting a black color background instead of image.. may i know the reason for this...?
somebody please help me.. regarding this issue......

推荐答案

ooviya写道:

ImageID =''"+ imageid +"''"

ImageID=''" +imageid+ "''"


ImageId是字符串字段吗?通常,ID字段不是.在这种情况下,您正在将一个字符串分配给int字段.应该是:


Is ImageId a string field? In general ID fields are not. If that''s the case, you are assigning a string to int field. It should be:

string q1 = "SELECT imgfile from myimages WHERE ImageID=" +imageid;


/// <summary>
/// function CopyDataToBitmap
/// Purpose: Given the pixel data return a bitmap of size [352,288],PixelFormat=24RGB
/// </summary>
/// <param name="data">Byte array with pixel data</param>
public Bitmap CopyDataToBitmap(byte[] data)
{
    //Here create the Bitmap to the know height, width and format
    Bitmap bmp = new Bitmap( 352, 288, PixelFormat.Format24bppRgb);
    //Create a BitmapData and Lock all pixels to be written
    BitmapData bmpData = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height)
                          ,ImageLockMode.WriteOnly, bmp.PixelFormat);
    //Copy the data from the byte array into BitmapData.Scan0
    Marshal.Copy(data, 0, bmpData.Scan0, data.Length);
    //Unlock the pixels
    bmp.UnlockBits(bmpData);
    //Return the bitmap
    return bmp;
}



解决方案基于:如何将byte []写入位图 [ ^ ]



Solution was based on: How to write byte[] to a Bitmap[^]


这篇关于从MySQL撷取图片时发生错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-22 14:14