這將是一個相當長的文本,所以請支持!發送和從數據庫中檢索BLOB之間的轉換錯誤
我試圖在面板上創建動態按鈕,顯示圖像。我想要在將圖像「上傳」到我的應用程序後一般創建圖像。我通過創建一個byte [] BLOB並將它發送到數據庫這樣做,並像這樣檢索它。
我會告訴你從創建到在這裏閱讀的完整路徑。首先,我通過打開的文件對話框檢索路徑並將其作爲變量發送。
OpenFileDialog ofd = new OpenFileDialog();
if (ofd.ShowDialog() == DialogResult.OK)
{
tbPictureLinkInput.Text = ofd.FileName;
}
buttonRepository.addButton(parent_id, console_id, tbPictureLinkInput.Text, tbWebsiteLinkInput.Text, tbNameInput.Text, buttontype);
然後,我創建了一個字節串這樣的:
byte[] imageArray = System.IO.File.ReadAllBytes(imagelink);
buttonContext.addButton(parent_id, console_id, Encoding.UTF8.GetString(imageArray), websitelink, tagname, buttontype.ToString());
的代碼,增加了數據到我的數據庫(不那麼重要)的部分:
public void addButton(int parent_id, int console_id, string imagelink, string websitelink, string tagname, string buttontype)
{
MySqlCommand command = new MySqlCommand();
command.CommandText = "INSERT INTO button(id, parent_id, console_id, imagelink, websitelink, tagname, buttontype) VALUES(null, @parent_id, @console_id, @imagelink, @websitelink, @tagname, @buttontype)";
command.Parameters.AddWithValue("parent_id", parent_id);
command.Parameters.AddWithValue("console_id", console_id);
command.Parameters.AddWithValue("imagelink", imagelink);
command.Parameters.AddWithValue("websitelink", websitelink);
command.Parameters.AddWithValue("tagname", tagname);
command.Parameters.AddWithValue("buttontype", buttontype);
Databasehandler.ExcecuteCommand(command);
}
public static bool ExcecuteCommand(MySqlCommand command)
{
try
{
using (MySqlConnection connection = new MySqlConnection())
{
int check;
connection.ConnectionString = connString;
connection.Open();
command.Connection = connection;
using (var transaction = connection.BeginTransaction())
{
command.Transaction = transaction;
check = command.ExecuteNonQuery();
transaction.Commit();
}
if (check > 0)
{
return true;
}
else
{
return false;
}
}
}
catch (MySqlException e)
{
Console.Write(e.Message);
return false;
}
catch (Exception e)
{
Console.Write(e.Message);
return false;
}
}
所以我databasetable看起來像這樣:
CREATE TABLE button(
id int(10) UNSIGNED auto_increment PRIMARY KEY,
parent_id int(10) UNSIGNED,
console_id int(10) UNSIGNED NOT NULL,
imagelink blob,
websitelink varchar(100),
tagname varchar(30) NOT NULL,
buttontype enum('Category', 'Website', 'non') NOT NULL,
FOREIGN KEY (console_id) REFERENCES console(id),
FOREIGN KEY (parent_id) REFERENCES button(id) ON DELETE CASCADE
);
那好吧我檢索數據再次ofcourse(對不起,我的代碼量,我只是不想讓你錯過任何東西,而閱讀本):
public List<Button> loadChildrenButtons(int parent_id, double convertSize)
{
MySqlCommand command = new MySqlCommand();
command.CommandText = "SELECT * FROM BUTTON WHERE Parent_id = @parent_id";
command.Parameters.AddWithValue("parent_id", parent_id);
return loadButtonsOnPanel(Databasehandler.SelectData(command), convertSize);
}
public static DataTable SelectData(MySqlCommand command)
{
try
{
DataTable dataTable = new DataTable();
using (MySqlConnection connection = new MySqlConnection())
{
connection.ConnectionString = connString;
connection.Open();
command.Connection = connection;
MySqlDataReader reader = command.ExecuteReader();
dataTable.Load(reader);
return dataTable;
}
}
catch (MySqlException e)
{
Console.Write(e.Message);
return null;
}
}
public Button dataTableToButton(DataRow row, int x, int y, double convertSize)
{
switch (row[6].ToString())
{
case "Category":
return (new Categorybutton(Convert.ToInt32(row[0]),
Convert.ToInt32(row[1]),
Convert.ToInt32(row[2]),
ObjectToByteArray(row[3]),
row[4].ToString(),
row[5].ToString(),
Conversions.convertButtonTypeToEnum(row[6].ToString()),
x,
y,
convertSize));
case "Website":
return (new Websitebutton(Convert.ToInt32(row[0]),
Convert.ToInt32(row[1]),
Convert.ToInt32(row[2]),
Convert.FromBase64String(row[3].ToString()),
row[4].ToString(),
row[5].ToString(),
Conversions.convertButtonTypeToEnum(row[6].ToString()),
x,
y,
convertSize));
}
return null;
}
public List<Button> loadButtonsOnPanel(DataTable table, double convertSize)
{
List<Button> buttons = new List<Button>();
int x = 24;
int y = 0;
int index = 0;
foreach (DataRow row in table.Rows)
{
if (index == 6)
{
x = x - 1032;
y = y + 288;
index = 0;
}
index = index + 1;
buttons.Add(dataTableToButton(row, Convert.ToInt32(Convert.ToDouble(x)*convertSize), Convert.ToInt32(Convert.ToDouble(y) * convertSize), convertSize));
x = x + 280;
}
return buttons;
}
這是怎麼了我的項目轉換爲數組:
byte[] ObjectToByteArray(object obj)
{
if (obj == null)
return null;
BinaryFormatter bf = new BinaryFormatter();
using (MemoryStream ms = new MemoryStream())
{
bf.Serialize(ms, obj);
return ms.ToArray();
}
}
最後這是我得到了ArgumentException
:
public Button(int id, int parent_id, int console_id, byte[] imagelink, string websitelink, string tagname, Enums.buttontype buttontype, int x, int y, double convertSize)
{
this.id = id;
this.parent_id = parent_id;
this.console_id = console_id;
this.imagelink = imagelink;
this.websitelink = websitelink;
this.tagname = tagname;
this.buttontype = buttontype;
this.x = x;
this.y = y;
Name = id.ToString();
this.Location = new System.Drawing.Point(x, y);
Height = Convert.ToInt32(240*convertSize);
Width = Convert.ToInt32(270 * convertSize);
BackgroundImage = ByteToImage(imagelink);
BackgroundImageLayout = ImageLayout.Stretch;
}
private static Bitmap ByteToImage(byte[] blob)
{
using (MemoryStream mStream = new MemoryStream())
{
mStream.Write(blob, 0, blob.Length);
mStream.Seek(0, SeekOrigin.Begin);
Bitmap bm = new Bitmap(mStream); //Right here.
return bm;
}
}
我對代碼的數量巨大的遺憾(如果你也能告訴我如何制定一個良好的快速q下次我會很感激的)。 在此先感謝!
編輯:
Exception thrown: 'System.ArgumentException' in System.Drawing.dll
System.ArgumentException: Parameter is not valid.
at System.Drawing.Bitmap..ctor(Stream stream)
at EasyOrder.Classes.Button.ByteToImage(Byte[] blob) in C:\Users\Freek\Desktop\Easy Order 2.0\EasyOrder\EasyOrder\Classes\Button.cs:line 55
你究竟在哪裏得到'ArgumentException'。同時發佈錯誤的完整堆棧跟蹤 – bc004346
該代碼在byte []和字符串之間有一些轉換,這對我來說似乎很奇怪,並且可能是錯誤的來源。看看這個[post](http://stackoverflow.com/a/43007416/2592875)的圖像存儲和檢索。 – TnTinMn
在底部添加堆棧報告。此外,錯誤在位圖bm =新的位圖(mStream); //就在這兒。但是代碼的錯誤更可能是在第一次轉換爲blob和string的時候。 –