473,409 Members | 1,970 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,409 software developers and data experts.

How to resize an image from SQL IMAGE field

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(FromF ile))
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.HorizontalResolutio n,
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.HighQualityBic ubic

' 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
Nov 18 '05 #1
2 2341
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.Rotate180Fli pNone);
processedBP.RotateFlip(RotateFlipType.Rotate180Fli pNone);

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" <Ro*******************@discussions.microsoft.com >
wrote in message news:05**********************************@microsof t.com...
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(FromF ile))
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.HorizontalResolutio n,
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.HighQualityBic ubic

' 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

Nov 18 '05 #2
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.Rotate180Fli pNone)
processedBP.RotateFlip(RotateFlipType.Rotate180Fli pNone)
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/utili...sharptovb.aspx

--

Regards,

Hermit Dave
(http://hdave.blogspot.com)
"Robson Carvalho Machado" <Ro*******************@discussions.microsoft.com >
wrote in message news:0E**********************************@microsof t.com...
Dear Friend

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

"Hermit Dave" wrote:
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

Nov 18 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: John Scott | last post by:
I am storing an image in an SQL database and have one field as an image datatype. I am also using a webservice to transport data. I want to be able to resize the image and pass back a thumbnail...
6
by: bissatch | last post by:
Hi, I have a collection of images stored in a DB. They are there for the purpose of a news system. When the user views the homepage it will diplay cropped versions of the news where the user...
5
by: Tamir Khason | last post by:
I have huge bitmap inmemory (about 4K x 6K 300dpi) and I want to quick resize it so old fasion way: Bitmap result = new Bitmap( nSize.Width, nSize.Height); using( Graphics g1 =...
3
by: Z D | last post by:
Hello, BACKGROUND: ============== I've created a Windows User Control that contains an Image Control (among other controls). The user control handles the picture resize event. Whenever the...
2
by: Carl Gilbert | last post by:
Hi I am looking for either a component or technique to allow me to do the following: * Provide a panel with a background image * Resize the image to best fit the panel to maintain aspect...
15
by: David Lozzi | last post by:
Howdy, I have a function that uploads an image and that works great. I love ..Nets built in upload, so much easier than 3rd party uploaders! Now I am making a public function that will take the...
2
by: Dominic Vella | last post by:
Hi, I know I seem to have the really complicated questions, but I guess that's why I'm here. This is a little verbose, only because I've been trying to crack this for a week now. Your help...
8
by: infoseekar | last post by:
Image Resize & Rotation Hi I have 2 scripts, one for Image rotation and other image resize and they both are working. Image resize scripts load the picture and resize it and Image rotation...
3
by: =?Utf-8?B?UiBSZXllcw==?= | last post by:
Hi! This discussion may help other programmers get a better idea of how to save uploaded images through a website. Why? Well currently, I save 3 versions of every uploaded image on my own...
22
by: simon2x1 | last post by:
i have an image which width is 213 and height is 200 when i echo the image and i resize it echo "<img src='company/$present' width='70' height='68'/>"; the image was not as clear as when it was...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.