How to resize an image from SQL IMAGE field
Messages   Related Types
This message was discovered on microsoft.public.dotnet.framework.aspnet.
Responses highlighted in red are from those people who are likely to be able to contribute good, authoratitive information to this discussion. They include Microsoft employees, MVP's and others who IMHO contribute well to these kinds of discussions.
Post a new message to this list...

Robson Carvalho Machado (VIP)
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
Reply to this message...
 
    
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)
"Robson Carvalho Machado" <Click here to reveal e-mail address>
wrote in message news:Click here to reveal e-mail address...
[Original message clipped]

Reply to this message...
 
    
Robson Carvalho Machado (VIP)
Dear Friend

Thanks for your try but I dont know how to translate C# to VB
Have you some VB example?

"Hermit Dave" wrote:

[Original message clipped]

Reply to this message...
 
    
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)
"Robson Carvalho Machado" <Click here to reveal e-mail address>
wrote in message news:Click here to reveal e-mail address...
[Original message clipped]

Reply to this message...
 
 
System.Drawing.Bitmap
System.Drawing.Drawing2D.CompositingQuality
System.Drawing.Drawing2D.InterpolationMode
System.Drawing.Drawing2D.PixelOffsetMode
System.Drawing.Drawing2D.SmoothingMode
System.Drawing.Graphics
System.Drawing.GraphicsUnit
System.Drawing.Image
System.Drawing.Imaging.ImageFormat
System.Drawing.Rectangle
System.Drawing.RotateFlipType
System.EventArgs
System.IO.FileInfo
System.IO.MemoryStream
System.Object
System.TimeSpan
System.Web.Caching.Cache
System.Web.Caching.CacheItemPriority
System.Web.Caching.CacheItemRemovedCallback
System.Web.HttpContext




Ad
MBR BootFX
Best-of-breed application framework for .NET projects, developed by Matthew Baxter-Reynolds and MBR IT
 
 Copyright © Matthew Baxter-Reynolds 2001-2008. '.NET 247 Software Development Services' is a trading style of MBR IT Solutions Ltd.
Contact Us - Terms of Use - Privacy Policy - www.dotnet247.com