How to resize an image from SQL IMAGE field

G

Guest

Dear Friends,

Does anybody knows how to use the below code with an image stored at SQL
Image field?


Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
ScaleStatic("/website/images/default_03.jpg", 50)
End Sub
Public Sub ScaleStatic(ByVal FromFile, ByVal intSize)
'Dim inputFile As New FileInfo(Server.MapPath(FromFile))
' Main bitmap/graphics "canvas". Change size to suit your needs
' load image
Dim imgPhoto As System.Drawing.Image =
System.Drawing.Image.FromFile(Server.MapPath(FromFile))
Dim sourceWidth As Integer = imgPhoto.Width
Dim sourceHeight As Integer = imgPhoto.Height
Dim sourceX As Integer = 0
Dim sourceY As Integer = 0
Dim destX As Integer = 0
Dim destY As Integer = 0
Dim destWidth As Integer = 0
Dim destHeight As Integer = 0

If imgPhoto.Width > imgPhoto.Height Then ' landscape-layout
' calculate new height
destHeight = ((intSize * 1.0 / sourceWidth) * sourceHeight)
destWidth = intSize
Else ' portrait-layout
destHeight = intSize
' calculate new width
destWidth = ((intSize * 1.0 / sourceHeight) * sourceWidth)
End If

Dim bmPhoto As Bitmap = New Bitmap(destWidth, destHeight,
imgPhoto.PixelFormat.Format24bppRgb)
bmPhoto.SetResolution(imgPhoto.HorizontalResolution,
imgPhoto.VerticalResolution)

Dim grPhoto As Graphics = Graphics.FromImage(bmPhoto)
grPhoto.CompositingQuality =
Drawing.Drawing2D.CompositingQuality.HighQuality
grPhoto.SmoothingMode = Drawing.Drawing2D.SmoothingMode.HighQuality
grPhoto.InterpolationMode =
Drawing.Drawing2D.InterpolationMode.HighQualityBicubic

' Draw onto canvas
grPhoto.DrawImage(imgPhoto, New Rectangle(destX, destY, destWidth,
destHeight), New Rectangle(sourceX, sourceY, sourceWidth, sourceHeight),
GraphicsUnit.Pixel)

' Stream to user
bmPhoto.Save(Response.OutputStream,
System.Drawing.Imaging.ImageFormat.Jpeg)

grPhoto.Dispose()
bmPhoto.Dispose()

End Sub
 
H

Hermit Dave

if its stored in sql server it will be stored as byte arry.. so read it into
a byte array.. attached find a piece of c# code that just does that.
use equivalent vb.net calls

private void WriteImage(ref byte[] ImageData, ref HttpContext context,
ImageType imgType, string cacheKey)
{
System.Drawing.Image myImage = null;
MemoryStream myStream = new MemoryStream();
MemoryStream processedMemStream = new MemoryStream();
try
{
myStream.Write(ImageData, 0, ImageData.Length);
myImage = System.Drawing.Image.FromStream(myStream);
int imageWidth = myImage.Width;
int imageHeight = myImage.Height;
int processedHeight;
if(imgType == ImageType.Full)
{
processedHeight = imageWidth;
}
else
{
processedHeight = (int)imgType;
}
double multiplicationFactor =
(double)processedHeight/(double)imageHeight;
int processedWidth = (int)( (double)imageWidth * multiplicationFactor);

Bitmap processedBP = new Bitmap(processedWidth, processedHeight);
Graphics g = Graphics.FromImage(processedBP);
try
{
g.SmoothingMode =SmoothingMode.HighQuality;
g.InterpolationMode =InterpolationMode.HighQualityBicubic;
g.PixelOffsetMode =PixelOffsetMode.HighQuality;

Rectangle rect=new Rectangle(0,0,processedWidth,processedHeight);
//Draw the old image on to the new image using the graphics object:

g.DrawImage(myImage,rect,0,0,myImage.Width,myImage.Height,GraphicsUnit.Pixel
);

processedBP.RotateFlip(RotateFlipType.Rotate180FlipNone);
processedBP.RotateFlip(RotateFlipType.Rotate180FlipNone);

processedBP.Save(processedMemStream, ImageFormat.Jpeg);
byte[] processedImageData = processedMemStream.ToArray();
if(processedImageData != null)
{
context.Cache.Add(cacheKey, processedImageData, null,
Cache.NoAbsoluteExpiration, TimeSpan.FromMinutes(10),
CacheItemPriority.Normal, new
CacheItemRemovedCallback(this.RemovedCallback));

context.Response.BinaryWrite(processedImageData);
processedImageData = null;
}
}
finally
{
g.Dispose();
processedBP.Dispose();
}
}
finally
{
processedMemStream.Close();
myStream.Close();
myImage.Dispose();
}
}

--

Regards,

Hermit Dave
(http://hdave.blogspot.com)
 
H

Hermit Dave

just did a c# to vb.net conversion

here's the result

Private Sub WriteImage(ByRef ImageData() As Byte, ByRef context As
HttpContext, ByVal imgType As ImageType, ByVal cacheKey As String)
Dim myImage As System.Drawing.Image = Nothing
Dim myStream As MemoryStream = New MemoryStream ()
Dim processedMemStream As MemoryStream = New MemoryStream ()
Try
myStream.Write(ImageData, 0, ImageData.Length)
myImage = System.Drawing.Image.FromStream(myStream)
Dim imageWidth As Integer = myImage.Width
Dim imageHeight As Integer = myImage.Height
Dim processedHeight As Integer
If imgType = ImageType.Full Then
processedHeight = imageWidth
Else
processedHeight = CType(imgType, Integer)
End If
Dim multiplicationFactor As Double = CType(processedHeight, Double) /
CType(imageHeight, Double)
Dim processedWidth As Integer = CType((CType(imageWidth, Double) *
multiplicationFactor), Integer)
Dim processedBP As Bitmap = New Bitmap (processedWidth, processedHeight)
Dim g As Graphics = Graphics.FromImage(processedBP)
Try
g.SmoothingMode = SmoothingMode.HighQuality
g.InterpolationMode = InterpolationMode.HighQualityBicubic
g.PixelOffsetMode = PixelOffsetMode.HighQuality
Dim rect As Rectangle = New Rectangle (0, 0, processedWidth,
processedHeight)
g.DrawImage(myImage, rect, 0, 0, myImage.Width, myImage.Height,
GraphicsUnit.Pixel)
processedBP.RotateFlip(RotateFlipType.Rotate180FlipNone)
processedBP.RotateFlip(RotateFlipType.Rotate180FlipNone)
processedBP.Save(processedMemStream, ImageFormat.Jpeg)
Dim processedImageData As Byte = processedMemStream.ToArray()
If Not (processedImageData Is Nothing) Then
context.Cache.Add(cacheKey, processedImageData, Nothing,
Cache.NoAbsoluteExpiration, TimeSpan.FromMinutes(10),
CacheItemPriority.Normal, New CacheItemRemovedCallback (Me.RemovedCallback))
context.Response.BinaryWrite(processedImageData)
processedImageData = Nothing
End If
Finally
g.Dispose()
processedBP.Dispose()
End Try
Finally
processedMemStream.Close()
myStream.Close()
myImage.Dispose()
End Try
End Sub

if you need to convert c# to vb.net use
http://www.developerfusion.com/utilities/convertcsharptovb.aspx

--

Regards,

Hermit Dave
(http://hdave.blogspot.com)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top