Click here to Skip to main content
15,881,757 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I need to read the image from the database and replace the original image on CANVAS.

The original way, the code is to read the coordinate value of the mouse click arbitrarily on CANVAS.

According to the coordinate value, it is converted into the ID of the image to be read, and the postback is triggered, so that C# can run the SQL code, and then the image is returned to HTML.


ASP.NET
//Storing coordinate values return to C# and converted into ID value
<asp:HiddenField ID="x_value" runat="server" Value="0"  />


//C# received image from the SQL and return to HTML
<asp:Image ID="Hidden_Coronal" runat="server" />


//After image processing, displayed on CANVAS
var Coronal_coor = document.getElementById("Image_Coronal_Coordinates").getContext("2d");

img_Coronal_coor = document.getElementById("Hidden_Coronal");
pixel_remove(Coronal_coor, img_Coronal_coor, 600, 600, 132, 0, 186, 600);
Coordinates(Coronal_coor, G_str, x_coor, 0);
Coordinates(Coronal_coor, R_hor, 0, y_coor);




But in this way, every time I change the image, I need to refresh the web page.
I hope I don’t need to refresh the webpage, I know AJAX can handle it, but after reading some articles, I’m still not sure how to use it.

The code I tried is as follows:
Can someone help me?

What I have tried:

I first try to read the image with a fixed ID value, and call the function directly


HTML
function changeIMG() {
    $.ajax({
        type: "POST",
        url: 'WebForm1.aspx/imagechange',
        data: "",
        //contentType: "application/json; charset=utf-8",
        //dataType: "json",
        success: function (msg) {
            $("#divResult").html("success");
        },
        error: function (e) {
            $("#divResult").html("Something Wrong.");
        }
    });

    var Coronal_bk = document.getElementById("ImageID").getContext("2d");
    img_Coronal_bk = document.getElementById("Hidden_Coronal_Background");
    Coronal_bk.drawImage(img_Coronal_bk, 0, 0, 186, 150);

}


C#
public void imagechange()
 {
     SqlConnection conn = new SqlConnection(con_database);

     SqlCommand Cor_command = conn.CreateCommand();
     conn.Open();
     Cor_command.Parameters.AddWithValue("@id", 5);
     Cor_command.CommandText = CorBack;
     using (SqlDataReader drCor = Cor_command.ExecuteReader())
     {
         if (drCor.HasRows)
         {
             while (drCor.Read())//嘗試讀取一筆資料列,並且回傳是否成功
             {

                 byte[] imagedata = (byte[])drCor["image"];
                 string img = Convert.ToBase64String(imagedata, 0, imagedata.Length);
                 Hidden_Coronal_Background.ImageUrl = "data:images/bmp;base64," + img;

             }

         }
         Cor_command.Parameters.Clear();
     }
     conn.Close();
 }
Posted
Comments
j snooze 14-Jul-21 17:25pm    
You haven't described what the result of your code does(errors?). However looking at your code one thing you may want to try (assuming the ajax call is working) is putting the last three lines of your imagechange function (var coronal_bk = document ....) into a completely separate function, and call that function in the "Success" section of the ajax code. Something like

success: function (msg) {
UpdateImage();
}

This will most likely be necessary because ajax is asynchronous and will run the last 3 lines in your "imagechange" function before the ajax call to the web server completes. By putting a function call on success, this tells javascript I want to run this function after my ajax call completes.

Hope this makes sense.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900