Friday, February 17, 2012

Display image stored in SQL

Hello group
Is there a way to display an image stored in SQL without using a datagrid? I am using vb, the very few examples I do find open the image in a window by its self. Also is there a way find the height and width of an image stored in the database and the ability to change the image size? Example – when an image is uploaded to the database I would like a thumbnail to be created and also stored in the database. Any ideas please help.
Michaelyup. there's a way to do all of the above. Man. Your questions are those with long long long responses. :-\


public Image ResizeImage(ref Image oImage, int ResizedWidth, int ResizedHeight, float Opacity)
{

// resize image.
System.Drawing.Bitmap oReturnVar = new System.Drawing.Bitmap(
ResizedWidth,
ResizedHeight,
System.Drawing.Imaging.PixelFormat.Format24bppRgb);

System.Drawing.Graphics oPhoto = System.Drawing.Graphics.FromImage(oReturnVar);

oPhoto.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
oPhoto.DrawImage(oImage,
new Rectangle(
-1,
-1,
ResizedWidth + 2,
ResizedHeight + 2),
0,
0,
oImage.Width,
oImage.Height,
System.Drawing.GraphicsUnit.Pixel,
CreateImageAttribute(Opacity));

oPhoto.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
oPhoto.Dispose();
oPhoto = null;
oImage.Dispose();
oImage = null;
return oReturnVar;
}

That's c#, but should be easy enough to do in vb.

http://www.dotnetspider.com/Technology/QA/ViewQuestion.aspx?QuestionId=117

the above link has a lot of examples on how to do the rest of what you wanted. Now your best bet is to learn the syntax of c#. Not really so you can write it, but more so you can be flexible in reading all the articles provided.

I like Dino Esposito's sample..


SqlCommand cmd = new SqlCommand(cmdText, cn);

MemoryStream ms = new
MemoryStream();

// 78 is the size of the OLE header
// for Northwind images.
// There's no header in PUBS as PUBS
// just contains the raw image bits.
int offset = 78;

cn.Open();
byte [] img = (byte[])
cmd.ExecuteScalar();
ms.Write(img, offset,
img.Length-offset);
cn.Close();

Bitmap bmp = null;
bmp = new Bitmap(ms);
Response.ContentType = "image/gif";
bmp.Save(Response.OutputStream,
ImageFormat.Gif);
ms.Close();

No comments:

Post a Comment