Hi IamAzhar,
You need to make a boolean method and check inside method whether bytes data is valid format or not.
Please see below code i have added a method is called IsValidImage() and inside method checking byte data and return is as true or false if true then it will add else is not going to be add.
Namespaces
using System.Data.SqlClient;
using System.Data;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Drawing.Imaging;
using System.Configuration;
Code
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["constr"].ConnectionString);
protected void Page_Load(object sender, EventArgs e)
{
SqlCommand cmd = new SqlCommand("SELECT Id,Data FROM tblfiles WHERE Id = @UserID", con);
cmd.Parameters.AddWithValue("@UserID", 1);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
byte[] imgbyte = (byte[])dt.Rows[0]["Data"];
string strBase71 = Convert.ToBase64String(imgbyte);
imgMypostprofile.ImageUrl = "data:Image/png;base64," + strBase71;
loadTap();
}
public void loadTap()
{
try
{
SqlCommand cmd = new SqlCommand("SELECT Id,Name,Data FROM tblfiles", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
for (int i = 0; i < dt.Rows.Count; i++)
{
HtmlGenericControl box1 = new HtmlGenericControl("div");
box1.Attributes.Add("class", "row");
HtmlGenericControl box2 = new HtmlGenericControl("div");
box2.Attributes.Add("class", "box box-widget");
HtmlGenericControl box3 = new HtmlGenericControl("div style='box-shadow:3px 3px 5px 6px #ccc;background-color:white;'");
box3.Attributes.Add("class", "box-footer box-comments");
HtmlGenericControl divspace1 = new HtmlGenericControl("div");
divspace1.Attributes.Add("class", "col-md-12");
HtmlGenericControl divspace2 = new HtmlGenericControl("div");
divspace2.Attributes.Add("class", "aligncenter");
HtmlGenericControl divspace3 = new HtmlGenericControl("div");
HtmlGenericControl divspaceImg = new HtmlGenericControl("div");
HtmlGenericControl divImg1 = new HtmlGenericControl("div");
divImg1.Attributes.Add("class", "col-md-12");
HtmlGenericControl divImg2 = new HtmlGenericControl("div");
divImg2.Attributes.Add("class", "aligncenter");
HtmlGenericControl divImg3 = new HtmlGenericControl("div");
divImg3.Attributes.Add("class", "tabcontent");
HtmlGenericControl div4Img1 = new HtmlGenericControl("div");
div4Img1.Attributes.Add("class", "col-md-12");
HtmlGenericControl div4Img2 = new HtmlGenericControl("div");
div4Img2.Attributes.Add("class", "aligncenter");
HtmlGenericControl div4Img3 = new HtmlGenericControl("div");
{
Image imageprofile = new Image();
byte[] imgbyte = (byte[])dt.Rows[i]["Data"];
string strBase65 = Convert.ToBase64String(imgbyte);
imageprofile.Attributes.Add("Class", "user-image");
imageprofile.ID = "imageProfile" + i;
imageprofile.ImageUrl = "data:Image/png;base64," + strBase65;
imageprofile.Width = Unit.Pixel(60);
imageprofile.Height = Unit.Pixel(60);
imageprofile.Style.Add(" border-radius", "60%");
imageprofile.Style.Add("margin", "1em");
imageprofile.Style.Add("margin-left", "-3em");
imageprofile.Style.Add("margin-bottom", "1.50em");
imageprofile.Style.Add(" border", "3px solid #fff");
Label lblName = new Label();
lblName.Text = dt.Rows[i]["Name"].ToString();
lblName.Style.Add("Font-Family", "Lucida Sans");
lblName.Attributes.Add("class", "control-label");
lblName.Style.Add("position", "absolute");
lblName.Style.Add("top", "12px");
lblName.Style.Add("font-size", "17px");
lblName.Style.Add("color", "#3c8dbc");
Label lblPostid = new Label();
lblPostid.ID = "lblPostid" + i;
lblPostid.Visible = false;
lblPostid.Text = dt.Rows[i]["Id"].ToString();
div4Img3.Controls.Add(imageprofile);
div4Img3.Controls.Add(lblName);
div4Img3.Controls.Add(lblPostid);
div4Img2.Controls.Add(div4Img3);
div4Img1.Controls.Add(div4Img2);
divImg3.Controls.Add(div4Img1);
divImg2.Controls.Add(divImg3);
divImg1.Controls.Add(divImg2);
divspaceImg.Controls.Add(divImg1);
}
//---------------------HTML div-----------------------------------//
HtmlGenericControl divspace4 = new HtmlGenericControl("div");
HtmlGenericControl divi1 = new HtmlGenericControl("div");
divi1.Attributes.Add("class", "col-sm-2");
HtmlGenericControl divi2 = new HtmlGenericControl("div");
divi2.Attributes.Add("class", "aligncenter");
HtmlGenericControl divi3 = new HtmlGenericControl("div");
divi3.Attributes.Add("class", "tab-pane");
divi3.Attributes.Add("class", "description-block");
divi3.Attributes.Add("class", "border-right 1px solid #fff");
LinkButton link1 = new LinkButton();
link1.ID = "link1" + i;
link1.Text = " Like";
link1.Style.Add("padding-left", "2.20em");
link1.Style.Add("padding-right", "2.20em");
link1.Style.Add("margin-bottom", "1em");
link1.Attributes.Add("runat", "server");
link1.CssClass = "btn btn-default btn-sm glyphicon glyphicon-thumbs-up";
link1.Click += new EventHandler(link1_Click);
divi3.Controls.Add(link1);
divi2.Controls.Add(divi3);
divi1.Controls.Add(divi2);
divspace4.Controls.Add(divi1);
//------------------------------HTML div----------------------------//
HtmlGenericControl divii1 = new HtmlGenericControl("div");
divii1.Attributes.Add("class", "col-sm-2");
HtmlGenericControl divii2 = new HtmlGenericControl("div");
divii2.Attributes.Add("class", "aligncenter");
HtmlGenericControl divii3 = new HtmlGenericControl("div");
divii3.Attributes.Add("class", "form-control");
divii3.Attributes.Add("class", "description-block");
divii3.Attributes.Add("class", "border-right 1px solid #fff");
LinkButton link2 = new LinkButton();
link2.ID = "link2" + i;
link2.Text = " Comment";
link2.Style.Add("padding-left", "1em");
link2.Style.Add("padding-right", "1em");
link2.CssClass = "btn btn-default btn-sm glyphicon glyphicon-comment";
link2.Style.Add("margin-bottom", "1em");
link2.Click += new EventHandler(link2_Click);
//------------------------------HTML div----------------------------//
HtmlGenericControl divpanel1 = new HtmlGenericControl("div");
divpanel1.Attributes.Add("class", "col-sm-12");
HtmlGenericControl divpanel2 = new HtmlGenericControl("div");
divpanel2.Attributes.Add("class", "aligncenter");
HtmlGenericControl divpanel3 = new HtmlGenericControl("div");
divpanel3.Attributes.Add("class", "tab-pane");
Panel Pnlcomment = new Panel();
Pnlcomment.ID = "Pnlcomment" + i.ToString();
Pnlcomment.CssClass = "panel panel-default";
Pnlcomment.Visible = false;
Pnlcomment.Height = Unit.Pixel(250);
Pnlcomment.Style.Add("Width", "100%");
Pnlcomment.Style.Add("margin", "1em");
Pnlcomment.Style.Add("margin-left", "-0.30em");
Pnlcomment.Style.Add("overflow-y", "auto");
Pnlcomment.Style.Add("border-radius", "12px");
Pnlcomment.Style.Add(" border", "3px solid #fff");
Pnlcomment.Style.Add("background-color", "white");
divpanel3.Controls.Add(Pnlcomment);
divpanel2.Controls.Add(divpanel3);
divpanel1.Controls.Add(divpanel2);
HtmlGenericControl divcom = new HtmlGenericControl("div");
HtmlGenericControl divcom1 = new HtmlGenericControl("div");
divcom1.Attributes.Add("class", "col-sm-2");
HtmlGenericControl divcom2 = new HtmlGenericControl("div");
divcom2.Attributes.Add("class", "aligncenter");
HtmlGenericControl divcom3 = new HtmlGenericControl("div");
divcom3.Attributes.Add("class", "tab-pane");
HtmlGenericControl divpnl1 = new HtmlGenericControl("div");
HtmlGenericControl divpnl2 = new HtmlGenericControl("div");
divpnl1.Attributes.Add("class", "col-sm-8");
HtmlGenericControl divpnl3 = new HtmlGenericControl("div");
divpnl3.Attributes.Add("class", "aligncenter");
HtmlGenericControl divpnl4 = new HtmlGenericControl("div");
SqlCommand cmd1 = new SqlCommand("SELECT Id,Name,Data FROM tblfiles", con);
SqlDataAdapter da1 = new SqlDataAdapter(cmd1);
DataTable dt1 = new DataTable();
da1.Fill(dt1);
GridView Gridview1 = new GridView();
for (int j = 0; j < dt1.Rows.Count; j++)
{
if (dt1.Rows.Count > 0)
{
if (!string.IsNullOrEmpty(dt1.Rows[j]["Data"].ToString()))
{
byte[] bytese = (byte[])dt1.Rows[j]["Data"];
string strBase66 = Convert.ToBase64String(bytese);
Image commentimage = new Image();
commentimage.ID = "commentimage_ " + i + "_" + j;
commentimage.ImageUrl = "data:Image/png;base64," + strBase66;
commentimage.Attributes.Add("class", "img-thumbnail");
commentimage.Style.Add("width", "55px");
commentimage.Style.Add("height", "55px");
commentimage.Style.Add("border-radius", "60%");
commentimage.Style.Add("margin", "0.50em");
commentimage.Style.Add("margin-left", "-7em");
commentimage.Style.Add("margin-bottom", "-3em");
commentimage.Style.Add(" border", "1.50px solid white");
Label tname = new Label();
tname.ID = "commentName_" + i + "_" + j;
tname.Text = dt1.Rows[j]["Name"].ToString() + " :";
tname.Style.Add("padding-top", "2em");
tname.Attributes.Add("class", "control-label");
tname.Style.Add("top", "8px");
tname.Style.Add("margin-left", "-1.25em");
tname.Style.Add("font-size", "auto");
tname.Style.Add("color", "Black");
Label lblcomment = new Label();
lblcomment.ID = "lblcomment_" + i + "_" + j;
lblcomment.Text = dt1.Rows[j]["Name"].ToString();
lblcomment.Attributes.Add("class", "control-label");
lblcomment.Style.Add("padding-top", "2em");
lblcomment.Style.Add("margin-right", "0.25em");
lblcomment.Style.Add("top", "8px");
lblcomment.Style.Add("font-size", "auto");
lblcomment.Style.Add("color", "Black");
divpnl4.Controls.Add(commentimage);
divpnl4.Controls.Add(new LiteralControl("<br/>"));
divpnl4.Controls.Add(tname);
divpnl4.Controls.Add(lblcomment);
divcom3.Controls.Add(new LiteralControl("<br/>"));
divpnl4.Controls.Add(new LiteralControl("<br/>"));
divpnl4.Controls.Add(new LiteralControl("<br/>"));
}
}
}
divcom2.Controls.Add(divcom3);
divcom1.Controls.Add(divcom2);
divcom.Controls.Add(divcom1);
Pnlcomment.Controls.Add(divcom);
divpnl3.Controls.Add(divpnl4);
divpnl2.Controls.Add(divpnl3);
divpnl1.Controls.Add(divpnl2);
Pnlcomment.Controls.Add(divpnl1);
divii3.Controls.Add(link2);
divii2.Controls.Add(divii3);
divii1.Controls.Add(divii2);
divspace4.Controls.Add(divii1);
HtmlGenericControl diviii1 = new HtmlGenericControl("div");
diviii1.Attributes.Add("class", "col-sm-2");
HtmlGenericControl diviii2 = new HtmlGenericControl("div");
diviii2.Attributes.Add("class", "aligncenter");
HtmlGenericControl diviii3 = new HtmlGenericControl("div");
diviii3.Attributes.Add("class", "tab-pane");
diviii3.Attributes.Add("class", "description-block");
diviii3.Attributes.Add("class", "border-right 1px solid #fff");
LinkButton link3 = new LinkButton();
link3.ID = "link3" + i;
link3.Text = " Delete";
link3.Style.Add("padding-left", "1.55em");
link3.Style.Add("padding-right", "1.55em");
link3.CssClass = "btn btn-default btn-sm glyphicon glyphicon-trash";
link3.Style.Add("margin-bottom", "1em");
link3.Click += new EventHandler(link3_click);
diviii3.Controls.Add(link3);
diviii2.Controls.Add(diviii3);
diviii1.Controls.Add(diviii2);
divspace4.Controls.Add(diviii1);
//----------------HTML div----------------------//
SqlCommand comand = new SqlCommand("SELECT Id, Name,Data FROM tblfiles", con);
SqlDataAdapter data = new SqlDataAdapter(comand);
DataTable dtable = new DataTable();
data.Fill(dtable);
Label lblDateTime = new Label();
lblDateTime.ID = "lblDateTime" + i;
lblDateTime.Text = dt.Rows[i]["Name"].ToString() + "-" + dt.Rows[i]["Name"].ToString();
lblDateTime.Attributes.Add("class", "control-label");
lblDateTime.Style.Add("font-size", "12px");
lblDateTime.Style.Add("color", "#999");
Label lblTitle = new Label();
lblTitle.ID = "lblTitle" + i;
lblTitle.Text = dt.Rows[i]["Name"].ToString();
lblTitle.Attributes.Add("class", "control-label");
lblTitle.Font.Bold = true;
lblTitle.Style.Add("margin-left", "-1.75em");
lblTitle.Style.Add("margin-bottom", "0.50em");
lblTitle.Style.Add("font-size", "14px");
lblTitle.Style.Add("color", "Black");
div4Img3.Controls.Add(new LiteralControl("<br/>"));
div4Img3.Controls.Add(lblTitle);
divspace3.Controls.Add(divspaceImg);
for (int k = 0; k < dtable.Rows.Count; k++)
{
byte[] byteees = (byte[])dtable.Rows[k]["Data"];
string strBase64 = Convert.ToBase64String(byteees);
HyperLink img1 = new HyperLink();
img1.Attributes.Add("class", "img-fluid img-thumbnail card");
img1.ID = "img1" + i + k;
img1.NavigateUrl = "data:Image/png;base64," + strBase64;
if (dtable.Rows.Count > 0)
{
bool IsTrue = IsValidImage(byteees);
if (IsTrue)
{
MemoryStream MemStream1 = new MemoryStream();
System.Drawing.Image im = System.Drawing.Image.FromStream(new MemoryStream(byteees));
System.Drawing.Image thumb = im.GetThumbnailImage(780, 420, () => false, IntPtr.Zero);
thumb.Save(MemStream1, ImageFormat.Png);
img1.Style.Add("width", "780");
string base64String = Convert.ToBase64String(MemStream1.ToArray());
img1.ImageUrl = "data:image/Png;base64," + base64String;
divspace3.Controls.Add(img1);
}
}
if (dtable.Rows.Count > 1)
{
HtmlGenericControl db0 = new HtmlGenericControl("div");
db0.Attributes.Add("class", "row");
HtmlGenericControl db1 = new HtmlGenericControl("div");
db1.Attributes.Add("class", "col-md-6");
HtmlGenericControl db2 = new HtmlGenericControl("div");
db2.Attributes.Add("class", "aligncenter");
HtmlGenericControl db3 = new HtmlGenericControl("div");
bool IsTrue = IsValidImage(byteees);
if (IsTrue)
{
MemoryStream MemStream1 = new MemoryStream();
System.Drawing.Image im = System.Drawing.Image.FromStream(new MemoryStream(byteees));
System.Drawing.Image thumb = im.GetThumbnailImage(380, 220, () => false, IntPtr.Zero);
thumb.Save(MemStream1, ImageFormat.Png);
string base64String = Convert.ToBase64String(MemStream1.ToArray());
img1.ImageUrl = "data:image/Png;base64," + base64String;
img1.Style.Add("width", "380");
img1.Style.Add("margin-left", "-0.65em");
img1.Style.Add("margin-bottom", "0.80em");
db2.Controls.Add(db3);
}
db2.Controls.Add(img1);
db1.Controls.Add(db2);
db1.Controls.Add(db0);
divspace3.Controls.Add(db1);
}
}
HtmlGenericControl div0 = new HtmlGenericControl("div");
div0.Attributes.Add("class", "row");
HtmlGenericControl div1 = new HtmlGenericControl("div");
div1.Attributes.Add("class", "col-sm-12");
HtmlGenericControl div2 = new HtmlGenericControl("div");
div2.Attributes.Add("class", "align-center");
HtmlGenericControl div3 = new HtmlGenericControl("div");
div3.Attributes.Add("class", "tabcontent");
TextBox text1 = new TextBox();
text1.ID = "text1" + i;
text1.Attributes.Add("class", "form-control");
text1.Style.Add("width", "100%");
text1.Attributes.Add("placeholder", "comment...");
text1.Style.Add("margin-top", "1em");
text1.Style.Add("margin-left", "-0.10em");
text1.Style.Add("margin-right", "1px");
text1.Style.Add("margin-bottom", "1em");
text1.TextChanged += new EventHandler(TextBox_TextChanged);
div2.Controls.Add(div3);
div2.Controls.Add(text1);
div1.Controls.Add(div2);
div1.Controls.Add(div0);
divspace3.Controls.Add(div1);
divspace3.Controls.Add(divspace4);
divspace3.Controls.Add(div3);
divspace3.Controls.Add(divpanel1);
divspace2.Controls.Add(divspace3);
divspace1.Controls.Add(divspace2);
box3.Controls.Add(divspace1);
box2.Controls.Add(box3);
box1.Controls.Add(box2);
PnlTap.Controls.Add(box1);
}
}
catch (Exception)
{
throw;
}
}
public static bool IsValidImage(byte[] bytes)
{
try
{
MemoryStream MemStream1 = new MemoryStream();
System.Drawing.Image im = System.Drawing.Image.FromStream(new MemoryStream(bytes));
System.Drawing.Image thumb = im.GetThumbnailImage(780, 420, () => false, IntPtr.Zero);
thumb.Save(MemStream1, ImageFormat.Png);
}
catch (ArgumentException)
{
return false;
}
return true;
}
public bool ThumbnailCallback()
{
return true;
}
protected void link1_Click(object sender, EventArgs e)
{
DataTable dt2 = (DataTable)Session["PostId"];
LinkButton link1 = sender as LinkButton;
PressedButton.Value = link1.ID;
if (link1.Text == "Like")
{
for (int i = 0; i < dt2.Rows.Count; i++)
{
LinkButton btnlike1 = (LinkButton)PnlTap.FindControl("link1" + i);
Label lblpost = (Label)PnlTap.FindControl("lblPostid" + i);
if (PressedButton.Value != null)
{
if (PressedButton.Value == "link1" + i)
{
SqlCommand comd = new SqlCommand("Imglikes", con);
comd.CommandType = System.Data.CommandType.StoredProcedure;
comd.Parameters.AddWithValue("@PostId", Convert.ToInt64(lblpost.Text).ToString());
con.Open();
comd.ExecuteNonQuery();
con.Close();
}
}
}
link1.Text = "unlike";
}
else if (link1.Text == "unike")
{
link1.Text = "Like";
}
}
protected void link2_Click(Object sender, EventArgs e)
{
DataTable dt2 = (DataTable)Session["PostId"];
LinkButton link2 = sender as LinkButton;
if (link2.Text == "Comment")
{
for (int i = 0; i < dt2.Rows.Count; i++)
{
Panel pnl = (Panel)PnlTap.FindControl("Pnlcomment" + i.ToString());
if (link2.ID == "link2" + i.ToString())
{
pnl.Visible = true;
link2.Text = "Comment -";
break;
}
}
}
else if (link2.Text == "Comment -")
{
for (int i = 0; i < dt2.Rows.Count; i++)
{
Panel pnl = (Panel)PnlTap.FindControl("Pnlcomment" + i.ToString());
if (link2.ID == "link2" + i.ToString())
{
pnl.Visible = false;
link2.Text = "Comment";
break;
}
}
}
}
protected void link3_click(Object sender, EventArgs e)
{
LinkButton link3 = sender as LinkButton;
DataTable dt2 = (DataTable)Session["PostId"];
PressedButton3.Value = link3.ID;
for (int i = 0; i < dt2.Rows.Count; i++)
{
LinkButton btnlike3 = (LinkButton)PnlTap.FindControl("link3" + i);
Label lblpost = (Label)PnlTap.FindControl("lblPostid" + i);
if (PressedButton3.Value != null)
{
if (PressedButton3.Value == "link3" + i)
{
SqlCommand comd = new SqlCommand("Spdeletepost", con);
comd.CommandType = System.Data.CommandType.StoredProcedure;
comd.Parameters.AddWithValue("@PostId", Convert.ToInt64(lblpost.Text).ToString());
con.Open();
comd.ExecuteNonQuery();
con.Close();
}
}
}
}
void TextBox_TextChanged(object sender, EventArgs e)
{
DataTable dt2 = (DataTable)Session["PostId"];
TextBox text1 = sender as TextBox;
for (int i = 0; i < dt2.Rows.Count; i++)
{
TextBox text2 = (TextBox)PnlTap.FindControl("text1" + i);
if (!String.IsNullOrWhiteSpace(text2.Text))
{
Label lblpost = (Label)PnlTap.FindControl("lblPostid" + i);
SqlCommand ccmd = new SqlCommand("ImgComment", con);
ccmd.CommandType = System.Data.CommandType.StoredProcedure;
ccmd.Parameters.AddWithValue("@Comments", text2.Text);
ccmd.Parameters.AddWithValue("@PostId", Convert.ToInt64(lblpost.Text).ToString());
ccmd.Parameters.AddWithValue("@UserID", Session["Usid"].ToString());
con.Open();
ccmd.ExecuteNonQuery();
con.Close();
text1.Text = string.Empty;
}
}
Response.Redirect(Request.Url.AbsoluteUri);
}