User1400794712 posted
Hi Rameezwaheed,
You can get the full url of the selected image from gridview, then using ReadBytes to convert the image to binary. I make a demo, you can refer to it.
<form>
<asp:GridView ID="gdImage" runat="server" HeaderStyle-BackColor="#3AC0F2" HeaderStyle-ForeColor="White"
AutoGenerateColumns="false">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="chkRow" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Image" ItemStyle-Width="150">
<ItemTemplate>
<asp:Image ID="Image1" runat="server" ImageUrl="Image Full URL" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:Button ID="btnGetSelected" runat="server" Text="Get selected records" OnClick="GetSelectedRecords" />
</form>
protected void GetSelectedRecords(object sender, EventArgs e)
{
foreach (GridViewRow row in gdImage.Rows)
{
if (row.RowType == DataControlRowType.DataRow)
{
CheckBox chkRow = (row.Cells[0].FindControl("chkRow") as CheckBox);
if (chkRow.Checked)
{
Image image = (Image)row.Cells[1].FindControl("Image1");
string path = image.ImageUrl;
string filename = Path.GetFileName(path);
FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read);
BinaryReader br = new BinaryReader(fs);
Byte[] bytes = br.ReadBytes((Int32)fs.Length); //Get the Binary of the image.
using (var db=new TestContext())
{
Images imageStore = new Images
{
FileName = filename,
FileBinary = bytes
};
db.Image.Add(imageStore);
db.SaveChanges();
}
}
}
}
}
Best Regards,
Daisy