Last post Oct 07, 2008 08:59 PM by Rivelyn
Oct 05, 2008 11:19 AM|Rivelyn|LINK
Hey All I've been mucking around with this for a bit now and can't seem to find a code example of how to resize an image based on how I am generating an image.
I have banner images stored in an MS SQL database, the banners are selected for use at the top of webpages, when creating a new webpage the user is presented with a radio button list that has each banner generated in an image control. Currently I am just
forcing the size of the image, with the image control and the page takes for ever for the images to load.
How can I force a resize of my images at render time to be more like thumbnails?, bellow is the code I am using on my page load event of the page that renders the images.
Oct 05, 2008 05:34 PM|chrisrock|LINK
This is a method I used in C#. Do you need it in VB?
// this is how to call the resizeimage method from an upload form
// I believe you can convert the myreader("banner_img") to a stream
// and use the System.Drawing.Image.FromStream method
private const int MAX_IMAGE_WIDTH_AND_HEIGHT = 200; byte fileArray = new Byte[profileImageUpload.PostedFile.ContentLength];
profileImageUpload.PostedFile.InputStream.Read(fileArray, 0, profileImageUpload.PostedFile.ContentLength);
System.Drawing.Image uploadedImage = System.Drawing.Image.FromStream(profileImageUpload.PostedFile.InputStream);
if (uploadedImage.Height > MAX_IMAGE_WIDTH_AND_HEIGHT || uploadedImage.Width > MAX_IMAGE_WIDTH_AND_HEIGHT)
fileArray = ResizeImage(uploadedImage);
private byte ResizeImage(System.Drawing.Image imageToResize)
Decimal percentageOfMaxWidth = (decimal)MAX_IMAGE_WIDTH_AND_HEIGHT / imageToResize.Width;
Decimal percentageOfMaxHeight = (decimal)MAX_IMAGE_WIDTH_AND_HEIGHT / imageToResize.Height;
Decimal whatPercentageToUse = percentageOfMaxWidth > percentageOfMaxHeight ? percentageOfMaxHeight : percentageOfMaxWidth;
System.Drawing.Bitmap newImage = new System.Drawing.Bitmap(imageToResize,
new System.Drawing.Size(Convert.ToInt32(imageToResize.Width * whatPercentageToUse),
Convert.ToInt32(imageToResize.Height * whatPercentageToUse)));
MemoryStream stream = new MemoryStream();
byte resizedImageArray = new Byte[stream.Length];
stream.Position = 0;
stream.Read(resizedImageArray, 0, (int)stream.Length);
Oct 06, 2008 03:59 PM|Rivelyn|LINK
Thanks Chris, I can convert it to VB, no problem. Let me have a look at it tonight and I will let you know how it works out for me.
Oct 07, 2008 06:17 PM|Rivelyn|LINK
I used to think I was pretty good at figuring out code, but wow this one is really starting to bother me. I can not figure this out, there is a lot of code examples out there and they all seem massive just to do a simple little re-size job. Also most of
the code out there deals with images stored in a file folder and not in a database.
I am truely stumped on how to get a full sized image from my sql database, resize it to a much smaller version then display it using VB. Why does this seem so difficult and labour intensive. The code above shows the extraction of the full sized image.
Oct 07, 2008 08:59 PM|Rivelyn|LINK
Maybe I should re-phrase the question.
Is there a way to get this
Into an Image.FileStream so I then can use the many code examples to resize the image when being rendered from an Image.FileStream.