简体   繁体   English

C#Picturebox.Image必须先加载两次,然后才能在代码中使用

[英]C# Picturebox.Image needs to be loaded twice before it can be used in code

I have a picturebox on a windows form that gets filled with this command: 我在Windows窗体上有一个图片框,里面装满了这个命令:

pBProcess.ImageLocation = Path.GetDirectoryName(processFile) + "\\" + processes[processStep2Nr] + ".png";

After the images is loaded I'm trying to do two things that both only work after I loaded that image twice. 加载图像后,我试图做两件事,它们只有在加载该图像两次后才能起作用。

First thing: 第一件事:

Positioning.CenterHorizontally(pBProcess, pBProcess.Image.Width);

Which does this: 这是做什么的:

public static void CenterHorizontally(this Control control, int Width)
{
    Rectangle parentRect = control.Parent.ClientRectangle;
    control.Left = (parentRect.Width - Width) / 2;
}

and the second thing I'm trying to do is to save the image to a SQLDatabase. 我要做的第二件事是将图像保存到SQLDatabase。

cmdLine = "INSERT INTO " + processToSave + " (Picture) VALUES  (@Image);";
MemoryStream stream = new MemoryStream();
pBProcess.Image.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
byte[] pic = stream.ToArray();
using (SqlCommand sqlCmd = new SqlCommand(cmdLine, connection))
{
//Parameter definieren
   sqlCmd.Parameters.Add("@Image", SqlDbType.Image);
   sqlCmd.Parameters["@Image"].Value = pic;
   sqlCmd.ExecuteNonQuery();
 }

Boths actions are executed correctly after I load the picturebox.image a second time. 在我第二次加载picturebox.image后,两个动作都正确执行。

What causes this and how can I fix it? 是什么导致这种情况,我该如何解决?

Edit: 编辑:

This is how I create the table I want to save the picture to: 这是我创建要将图片保存到的表的方式:

//Tabelle wird erstellt und anschließend gefüllt
strInsert = "CREATE TABLE " + processToSave + "(Attributes NVARCHAR(50), Value TINYINT, Picture IMAGE);";
using (SqlCommand sqlCmd = new SqlCommand(strInsert, connection))
{
    sqlCmd.ExecuteNonQuery();
}
strInsert = "INSERT INTO " + processToSave + " (Attributes, Value) VALUES (@Attributes, @Value);";
foreach (ListViewItem item in checkedItems)
{
    using (SqlCommand sqlCmd = new SqlCommand(strInsert, connection))
    {
        //Parameter definieren
        sqlCmd.Parameters.Add("@Attributes", SqlDbType.NVarChar);
        sqlCmd.Parameters["@Attributes"].Value = item.Text;
        sqlCmd.Parameters.Add("@Value", SqlDbType.Int);
        sqlCmd.Parameters["@Value"].Value = Convert.ToInt32(item.SubItems[1].Text);
        sqlCmd.ExecuteNonQuery();
    }
}
strInsert = "INSERT INTO " + processToSave + " (Picture) VALUES  (@Image);";
MemoryStream stream = new MemoryStream();
pBProcess.Image.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
byte[] pic = stream.ToArray();
using (SqlCommand sqlCmd = new SqlCommand(strInsert, connection))
{
    //Parameter definieren
    sqlCmd.Parameters.Add("@Image", SqlDbType.Image);
    sqlCmd.Parameters["@Image"].Value = pic;
    sqlCmd.ExecuteNonQuery();
}

And this is how I read the picture from the DB: 这就是我从DB中读取图片的方式:

MemoryStream stream = new MemoryStream();
strInsert = "SELECT [Picture] FROM " + processToLoad;
SqlCommand sqlCmd2 = new SqlCommand(strInsert, connection);
byte[] image = (byte[])sqlCmd2.ExecuteScalar();
stream.Write(image, 0, image.Length);
Bitmap bitmap = new Bitmap(stream);
pBProcess.Image = bitmap;

this is NOT working. 这是行不通的。 But when I create a table in the DB called "Bilder" which has only "Picture" with DBType Image and change the code to this: 但是,当我在数据库中创建一个名为“ Bilder”的表时,该表仅具有“ Picture”和DBType Image,并将代码更改为此:

strInsert = "INSERT INTO Bilder (Picture) VALUES  (@Image);";
MemoryStream stream = new MemoryStream();
pBProcess.Image.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
byte[] pic = stream.ToArray();
using (SqlCommand sqlCmd = new SqlCommand(strInsert, connection))
{
    //Parameter definieren
    sqlCmd.Parameters.Add("@Image", SqlDbType.Image);
    sqlCmd.Parameters["@Image"].Value = pic;
    sqlCmd.ExecuteNonQuery();
}

...

MemoryStream stream = new MemoryStream();
strInsert = "SELECT [Picture] FROM Bilder";
SqlCommand sqlCmd2 = new SqlCommand(strInsert, connection);
byte[] image = (byte[])sqlCmd2.ExecuteScalar();
stream.Write(image, 0, image.Length);
Bitmap bitmap = new Bitmap(stream);
pBProcess.Image = bitmap;

It actually works. 它确实有效。 What's wrong in my original code? 我的原始代码有什么问题?

I was mixing up two problems here. 我在这里混淆了两个问题。

The problem with 这个问题

Positioning.CenterHorizontally(pBProcess, pBProcess.Image.Width);

was fixed by putting this statement into the LoadCompleted Event of the picturebox. 通过将此语句放入图片LoadCompleted事件来修复。

The second problem was that I actually inserted a lot of rows into my table (attributes + value) and at the end I inserted another row, consisting only of the image. 第二个问题是我实际上在表中插入了很多行(属性+值),最后我又插入了另一行,仅包含图像。 When I read the table looking for the image, I only read the first row (I think) but there was no image to be found. 当我阅读表格以查找图像时,我只读了第一行(我认为),但是找不到图像。 So I changed the code, putting the image into the first row and then changing the commandline: 所以我更改了代码,将图像放入第一行,然后更改命令行:

bool first = true;
foreach (ListViewItem item in checkedItems)
{
   if (first)
     cmdLine = "INSERT INTO " + processToSave + " (Attributes, Value, Picture) VALUES (@Attributes, @Value, @Image);";
   else
     cmdLine = "INSERT INTO " + processToSave + " (Attributes, Value) VALUES (@Attributes, @Value);";
   using (SqlCommand sqlCmd = new SqlCommand(cmdLine, connection))
   {
     //Parameter definieren
     sqlCmd.Parameters.Add("@Attributes", SqlDbType.NVarChar);
     sqlCmd.Parameters["@Attributes"].Value = item.Text;
     sqlCmd.Parameters.Add("@Value", SqlDbType.Int);
     sqlCmd.Parameters["@Value"].Value = Convert.ToInt32(item.SubItems[1].Text);
     if (first)
     {
        sqlCmd.Parameters.Add("@Image", SqlDbType.Image);
        sqlCmd.Parameters["@Image"].Value = pic;
        first = false;
     }
     sqlCmd.ExecuteNonQuery();
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM