簡體   English   中英

我如何通過404異常從數據表C#winform中從遠程服務器加載圖像

[英]how can i load the images from remote server in a datatable C# winform throughs an exception of 404

如何以數據表C#Windows窗體從遠程服務器加載圖像? 我僅將圖像路徑存儲在Sqldatabase中。 當我運行代碼時,它將引發404錯誤異常。 相同的代碼正在檢索其他形式的圖像

屏幕截圖

 private void Datagrid() { 
                try
                {
                    id.Text = "";
                    name.Text = "";
                    cost.Text = "";

                    price.Text = "";
                    description.Text = "";
                    status.Text = "";
                    picture.Image = null;

                    status.Items.Add("active");
                    status.Items.Add("inactive");
                    id.Visible = false;
                    label7.Text = "";
                    conn.Open();
                    MySqlCommand cm = new MySqlCommand();
                   // string query = "SELECT dealSuggestion_id,name,cost,price,description,Status from dealSuggestion where Status='inactive' LIMIT 8";
                    string query = "SELECT * from dealSuggestion where Status='inactive' LIMIT 8";

                    cm.CommandText = query;
                    cm.Connection = conn;
                    MySqlDataAdapter da = new MySqlDataAdapter(cm);
                    DataTable dt = new DataTable();
                    da.Fill(dt);
foreach (DataRow row in dt.Rows)
                {
                    string img = row["image"].ToString();

                    if (!row.IsNull("image"))
                    {
                     ServicePointManager.Expect100Continue = true;
                        System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
                        System.Net.WebRequest request = System.Net.WebRequest.Create(row["image"].ToString());
                        System.Net.WebResponse resp = request.GetResponse();
                        System.IO.Stream respStream = resp.GetResponseStream();
                        Bitmap bmp = new Bitmap(respStream);
                        respStream.Dispose();
                        row["image"] = bmp;

                    }
                    }
                    dataGridView1.DataSource = dt;
                    conn.Close();
                }
                catch (Exception ex)
                {

                    MessageBox.Show("Error" + ex);
                }
            }
    Imports System 
 Imports System.Runtime.InteropServices
Imports System.Security.Principal  Imports System.Security.Permissions  Public Class Form1  <DllImport("advapi32.DLL", SetLastError:=True)> _  Public Shared Function LogonUser(ByVal lpszUsername As String, ByVal lpszDomain As String, _ 
        ByVal lpszPassword As String, ByVal dwLogonType As Integer, ByVal dwLogonProvider As Integer, _ 
        ByRef phToken As IntPtr) As Integer  End Function  Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click 
        Dim admin_token As IntPtr 
        Dim wid_current As WindowsIdentity = WindowsIdentity.GetCurrent() 
        Dim wid_admin As WindowsIdentity = Nothing 
        Dim wic As WindowsImpersonationContext = Nothing 
        Try 

            If LogonUser("Local Admin name", "Local computer name", "pwd", 9, 0, admin_token) <> 0 Then 
                wid_admin = New WindowsIdentity(admin_token) 
                wic = wid_admin.Impersonate() 

// inside this scope you can access remote PC please note that path shouldn't contain : instead of : use $ sign
              FileStream fs = new FileStream(imagepath, FileMode.Open, FileAccess.Read);

    //Initialize a byte array with size of stream
    byte[] imgByteArr = new byte[fs.Length];

    //Read data from the file stream and put into the byte array
    fs.Read(imgByteArr, 0, Convert.ToInt32(fs.Length));

    //Close a file stream
    fs.Close();

            Else 
               //custom message
            End If 
        Catch se As System.Exception 
            Dim ret As Integer = Marshal.GetLastWin32Error() 
            MessageBox.Show(ret.ToString(), "Error code: " + ret.ToString()) 
            MessageBox.Show(se.Message) 
        Finally 
            If wic IsNot Nothing Then 
                wic.Undo() 
            End If 
        End Try  
        End Sub  
        End Class

試試這個先生,請投票,如果它的工作謝謝你

    FileStream fs = new FileStream(imagepath, FileMode.Open, FileAccess.Read);

    //Initialize a byte array with size of stream
    byte[] imgByteArr = new byte[fs.Length];

    //Read data from the file stream and put into the byte array
    fs.Read(imgByteArr, 0, Convert.ToInt32(fs.Length));

    //Close a file stream
    fs.Close();

    //assign to dt particular column imageByteArr

希望這個能對您有所幫助

我認為這是一個foem工具。 您可以嘗試在此問題中描述的類似內容:

 foreach (DataRow row in t.Rows)
{
                HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(row["uri"].ToString());
                myRequest.Method = "GET";
                HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
                System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(myResponse.GetResponseStream());
                myResponse.Close();

                row["Img"] = bmp;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM