Display good quality image

Posted by Chandan.sahab under Regular Expressions on 12/3/2009 | Views : 1298 | Status : [Member] | Replies : 5
Hi,

I have inserted images in database (Sql 2005), and its going properly. But when i'm going to dispaly that images on the page in gridview, the quality of image is not as what i have uploaded. i.e i'm getting that image of poor quality.
So Please suggest me how to resolve it, Or provide me any alternate method for saving images and displaying it properly.


Thanks in Advance

Chandan kumar




Responses

Posted by: Vuyiswamb on: 12/3/2009 [Member] [MVP] [Administrator] NotApplicable

Up
0
Down
Can we see the code you use to save and retrieve

Thank you for posting at Dotnetfunda
[Administrator]

Chandan.sahab, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Chandan.sahab on: 12/3/2009 [Member] Starter

Up
0
Down
Hi Vuyiswamb,
thanks, here is the code.

***For Insertion

int len = FileUpload1.PostedFile.ContentLength;
byte[] pic = new byte[len];
FileUpload1.PostedFile.InputStream.Read(pic, 0, len);


public int InserImage(int prodId, string ItemName, string ItemCode, double ItemPrice, string ItemDesc, byte[] pic,
string itemsize, int vendorId, double vendorprice, double QsourcePrice)
{

string ssql = string.Empty;
int i = 0;
SqlConnection objcon = null;
SqlCommand objcommd = null;

objcon = new SqlConnection(SQL.Connection.SqlConnection.ConnectionString);
objcommd = new SqlCommand("Insert Into MstItemMaster(FKProdID,ItemName,ItemCode,ItemDesc,ItemImage,ItemPrice,itemSize,FkVendorId,VendorPrice,QsourcePrice )" +
" values(@FKProdID,@ItemName,@ItemCode,@ItemDesc,@ItemImage,@ItemPrice,@itemSize,@FkVendorId,@VendorPrice,@sourcePrice)", objcon);
//objcon.CreateCommand();
//ssql = "Insert Into MstProduct(ProdName,ProdDesc,CatId,ProdImage,Price) "+
// " values ('"+prodName+"','"+ProdDesc+"',"+catId+","+pic+","+prodPrice+")";

try
{
SqlParameter ParamProdName = new SqlParameter("@ItemName", SqlDbType.VarChar);
ParamProdName.Value = ItemName;
objcommd.Parameters.Add(ParamProdName);

SqlParameter ParamProdImg = new SqlParameter("@ItemImage", SqlDbType.Image);
ParamProdImg.Value = (byte[])pic;
objcommd.Parameters.Add(ParamProdImg);

SqlParameter ParamProdDesc = new SqlParameter("@ItemDesc", SqlDbType.VarChar);
ParamProdDesc.Value = ItemDesc;
objcommd.Parameters.Add(ParamProdDesc);

SqlParameter ParamPrice = new SqlParameter("@ItemPrice", SqlDbType.Decimal);
ParamPrice.Value = ItemPrice;
objcommd.Parameters.Add(ParamPrice);

SqlParameter ParamprodId = new SqlParameter("@FKProdID", SqlDbType.Int);
ParamprodId.Value = prodId;
objcommd.Parameters.Add(ParamprodId);

SqlParameter ParamItemCode = new SqlParameter("@ItemCode", SqlDbType.VarChar);
ParamItemCode.Value = ItemCode;
objcommd.Parameters.Add(ParamItemCode);


SqlParameter ParamItemSize = new SqlParameter("@ItemSize", SqlDbType.VarChar);
ParamItemSize.Value = itemsize;
objcommd.Parameters.Add(ParamItemSize);

SqlParameter ParamItemVendor = new SqlParameter("@FkVendorId", SqlDbType.Int);
ParamItemVendor.Value = vendorId;
objcommd.Parameters.Add(ParamItemVendor);

SqlParameter ParamItemVendorPrice = new SqlParameter("@VendorPrice", SqlDbType.Decimal);
ParamItemVendorPrice.Value = vendorprice;
objcommd.Parameters.Add(ParamItemVendorPrice);


SqlParameter ParamItemQsourcePrice = new SqlParameter("@sourcePrice", SqlDbType.Decimal);
ParamItemQsourcePrice.Value = sourcePrice;
objcommd.Parameters.Add(ParamItemQsourcePrice);

objcon.Open();
i = objcommd.ExecuteNonQuery();
//objcon.Close();
}

// i= SQL.Command.ExecuteNonQuery(ssql, CommandType.Text, null);

catch (Exception ex)
{
throw new Exception(ex.Message);
}
finally
{
objcon.Close();


}

return i;


}

//////////////////////////////////////////////////
For Retrieving in gridview

On .aspx page
----------------------------------------------------
<asp:DataList ID="dataItem" runat="server" RepeatColumns="4" RepeatDirection="Horizontal"
RepeatLayout="Table" Width="700px" DataKeyField="PkItemId" BackColor="#cccccc">
<ItemTemplate>
<table class="">
<tr>
<td align="center">
<asp:Image ID="ItemImage" ImageUrl='<%# "~/masters/viewItem.aspx?ItemId="+ Eval("PkItemId") %>'
ToolTip='<%#Eval("PkItemId") %>' Height="125px" Width="125px" runat="server" />
</td>
</tr>
<tr>
<td align="center">
Item Name:<asp:Label ID="lblItemName" runat="server" Text='<% #Eval("ItemName") %>'></asp:Label>
</td>
</tr>
<tr>
<td align="center">
Item Code:<asp:Label ID="lblItemCode" runat="server" Text='<%#Eval("ItemCode") %>'></asp:Label>
</td>
</tr>
<tr>
<td align="center">
<%--'<%#Eval("ItemPrice") %>'--%>
Price:<asp:Label ID="lblItemPrice" runat="server" Text="On Request"></asp:Label>
</td>
</tr>
<tr>
<td align="center">
Description:<asp:Label ID="lblItemDesc" runat="server" Text='<%#Eval("ItemDesc") %>'></asp:Label>
</td>
</tr>
<tr>
<td align="center">
Select


Chandan.sahab, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Chandan.sahab on: 12/3/2009 [Member] Starter

Up
0
Down
//for retrieving
protected void Page_Load(object sender, EventArgs e)
{
if (Request.Params["ItemId"] != null)
{

SqlConnection objcon = null;
SqlCommand objcommd = null;
MemoryStream stream = new MemoryStream();
objcon = new SqlConnection(SQL.Connection.SqlConnection.ConnectionString);
objcommd = new SqlCommand("select ItemImage from MstItemMaster where PkItemId=" + Convert.ToInt16(Request.Params["ItemId"]) + "", objcon);
objcon.Open();
byte[] image = (byte[])objcommd.ExecuteScalar();
stream.Write(image, 0, image.Length);
Bitmap bitmap = new Bitmap(stream);
Response.ContentType = "image/gif";
bitmap.Save(Response.OutputStream, ImageFormat.Gif);


}

}

Chandan.sahab, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Vuyiswamb on: 12/3/2009 [Member] [MVP] [Administrator] NotApplicable

Up
0
Down
would it be a Trainsmash if you change the image Types from Gif to PNG ?

Thank you for posting at Dotnetfunda
[Administrator]

Chandan.sahab, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Chandan.sahab on: 12/7/2009 [Member] Starter

Up
0
Down
Hi Vuyiswamb,

Thanks for your reply. As i've resolved by using the extension JPEG istead of png and gif.........
thanks a lot...


Chandan.sahab, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response