hi
these are code that i use for reduce image size without streaching image
My problem is when i run website in my gridview doesn't show image it just show image border but doesn't show my image from database
image.aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
SqlCommand _cmd = new SqlCommand("select image from House_p where behcode=1115", _cn);
_cn.Open();
SqlDataReader _dr = _cmd.ExecuteReader();
if (_dr.HasRows)
{
GridView1.DataSource = _dr;
GridView1.DataBind();
}
_cn.Close();
}
public string img_resize(string picname, int maxHeight, int maxWidth)
{
System.Drawing.Image currentImage = System.Drawing.Image.FromFile(Server.MapPath("image/house\\") + picname);
double imgHeight = 0;
double imgWidth = 0;
imgHeight = currentImage.Height;
imgWidth = currentImage.Width;
if (imgWidth > maxWidth | imgHeight > maxHeight)
{
//Determine what dimension is off by more
double deltaWidth = imgWidth - maxWidth;
double deltaHeight = imgHeight - maxHeight;
double scaleFactor = 0;
if (deltaHeight > deltaWidth)
{
//Scale by the height
scaleFactor = maxHeight / imgHeight;
}
else
{
//Scale by the Width
scaleFactor = maxWidth / imgWidth;
}
imgWidth *= scaleFactor;
imgHeight *= scaleFactor;
}
string html = null;
if (imgHeight != currentImage.Height | imgWidth != currentImage.Width) {
html = "<a href=\"" + Path.GetFileName("../image/house") + "\">" + "<img src=\"ShowThumbnail.aspx?img=" + Path.GetFileName("../image/house") + "&w=" + imgWidth + "&h=" + imgHeight + "\" " + "height=\"" + imgHeight + "\" width=\"" + imgWidth + "\" border=1>" + "</a>";
} else {
html = "<a href=\""+ Path.GetFileName("../image/house") + "\">" + "<img src=\"ShowThumbnail.aspx?img=" + Path.GetFileName("../image/house") + "\" " + "height=\"" + imgHeight + "\" width=\"" + imgWidth + "\" border=1>" + "</a>";
}
return html;
image.aspx
<div>
<asp:GridView ID="GridView1" runat="server" AllowSorting="true" AutoGenerateColumns="false" Width="100%" GridLines="None">
<Columns>
<asp:TemplateField SortExpression="reply_user">
<ItemTemplate>
<%#img_resize(Eval("image").ToString(),100,80 )%>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
ShowThumpnail.aspx
protected void Page_Load(object sender, EventArgs e)
{
string imageUrl = Request.QueryString["img"];
int imageHeight = int.Parse(Request.QueryString["h"]);
int imageWidth = int.Parse(Request.QueryString["w"]);
if (imageUrl.IndexOf("/") >= 0 | imageUrl.IndexOf("\\") >= 0)
{
//We found a / or \
Response.End();
}
imageUrl = "image/house\\" + imageUrl;
System.Drawing.Image fullSizeImg = null;
fullSizeImg = System.Drawing.Image.FromFile(Server.MapPath(imageUrl));
Response.ContentType = "image/jpg";
if (imageHeight > 0 & imageWidth > 0)
{
System.Drawing.Image.GetThumbnailImageAbort dummyCallBack = null;
dummyCallBack = new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback);
System.Drawing.Image thumbNailImg = null;
thumbNailImg = fullSizeImg.GetThumbnailImage(imageWidth, imageHeight, dummyCallBack, IntPtr.Zero);
fullSizeImg.Save(Response.OutputStream, ImageFormat.Gif);
}
else
{
fullSizeImg.Save(Response.OutputStream, ImageFormat.Gif);
}
}
public bool ThumbnailCallback()
{
return false;
}
}
and my image is in this directoty
image/House