473,566 Members | 2,812 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.EventArg s) Handles MyBase.Load
ScaleStatic("/website/images/default_03.jpg" , 50)
End Sub
Public Sub ScaleStatic(ByV al FromFile, ByVal intSize)
'Dim inputFile As New FileInfo(Server .MapPath(FromFi le))
' 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(destWidt h, destHeight,
imgPhoto.PixelF ormat.Format24b ppRgb)
bmPhoto.SetReso lution(imgPhoto .HorizontalReso lution,
imgPhoto.Vertic alResolution)

Dim grPhoto As Graphics = Graphics.FromIm age(bmPhoto)
grPhoto.Composi tingQuality =
Drawing.Drawing 2D.CompositingQ uality.HighQual ity
grPhoto.Smoothi ngMode = Drawing.Drawing 2D.SmoothingMod e.HighQuality
grPhoto.Interpo lationMode =
Drawing.Drawing 2D.Interpolatio nMode.HighQuali tyBicubic

' Draw onto canvas
grPhoto.DrawIma ge(imgPhoto, New Rectangle(destX , destY, destWidth,
destHeight), New Rectangle(sourc eX, sourceY, sourceWidth, sourceHeight),
GraphicsUnit.Pi xel)

' Stream to user
bmPhoto.Save(Re sponse.OutputSt ream,
System.Drawing. Imaging.ImageFo rmat.Jpeg)

grPhoto.Dispose ()
bmPhoto.Dispose ()

End Sub
Nov 18 '05 #1
2 2353
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 processedMemStr eam = new MemoryStream();
try
{
myStream.Write( ImageData, 0, ImageData.Lengt h);
myImage = System.Drawing. Image.FromStrea m(myStream);
int imageWidth = myImage.Width;
int imageHeight = myImage.Height;
int processedHeight ;
if(imgType == ImageType.Full)
{
processedHeight = imageWidth;
}
else
{
processedHeight = (int)imgType;
}
double multiplicationF actor =
(double)process edHeight/(double)imageHe ight;
int processedWidth = (int)( (double)imageWi dth * multiplicationF actor);

Bitmap processedBP = new Bitmap(processe dWidth, processedHeight );
Graphics g = Graphics.FromIm age(processedBP );
try
{
g.SmoothingMode =SmoothingMode. HighQuality;
g.Interpolation Mode =InterpolationM ode.HighQuality Bicubic;
g.PixelOffsetMo de =PixelOffsetMod e.HighQuality;

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

g.DrawImage(myI mage,rect,0,0,m yImage.Width,my Image.Height,Gr aphicsUnit.Pixe l
);

processedBP.Rot ateFlip(RotateF lipType.Rotate1 80FlipNone);
processedBP.Rot ateFlip(RotateF lipType.Rotate1 80FlipNone);

processedBP.Sav e(processedMemS tream, ImageFormat.Jpe g);
byte[] processedImageD ata = processedMemStr eam.ToArray();
if(processedIma geData != null)
{
context.Cache.A dd(cacheKey, processedImageD ata, null,
Cache.NoAbsolut eExpiration, TimeSpan.FromMi nutes(10),
CacheItemPriori ty.Normal, new
CacheItemRemove dCallback(this. RemovedCallback ));

context.Respons e.BinaryWrite(p rocessedImageDa ta);
processedImageD ata = null;
}
}
finally
{
g.Dispose();
processedBP.Dis pose();
}
}
finally
{
processedMemStr eam.Close();
myStream.Close( );
myImage.Dispose ();
}
}

--

Regards,

Hermit Dave
(http://hdave.blogspot.com)
"Robson Carvalho Machado" <Ro************ *******@discuss ions.microsoft. com>
wrote in message news:05******** *************** ***********@mic rosoft.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.EventArg s) Handles MyBase.Load
ScaleStatic("/website/images/default_03.jpg" , 50)
End Sub
Public Sub ScaleStatic(ByV al FromFile, ByVal intSize)
'Dim inputFile As New FileInfo(Server .MapPath(FromFi le))
' 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(destWidt h, destHeight,
imgPhoto.PixelF ormat.Format24b ppRgb)
bmPhoto.SetReso lution(imgPhoto .HorizontalReso lution,
imgPhoto.Vertic alResolution)

Dim grPhoto As Graphics = Graphics.FromIm age(bmPhoto)
grPhoto.Composi tingQuality =
Drawing.Drawing 2D.CompositingQ uality.HighQual ity
grPhoto.Smoothi ngMode = Drawing.Drawing 2D.SmoothingMod e.HighQuality grPhoto.Interpo lationMode =
Drawing.Drawing 2D.Interpolatio nMode.HighQuali tyBicubic

' Draw onto canvas
grPhoto.DrawIma ge(imgPhoto, New Rectangle(destX , destY, destWidth,
destHeight), New Rectangle(sourc eX, sourceY, sourceWidth, sourceHeight),
GraphicsUnit.Pi xel)

' Stream to user
bmPhoto.Save(Re sponse.OutputSt ream,
System.Drawing. Imaging.ImageFo rmat.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(ByRe f 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 processedMemStr eam As MemoryStream = New MemoryStream ()
Try
myStream.Write( ImageData, 0, ImageData.Lengt h)
myImage = System.Drawing. Image.FromStrea m(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 multiplicationF actor As Double = CType(processed Height, Double) /
CType(imageHeig ht, Double)
Dim processedWidth As Integer = CType((CType(im ageWidth, Double) *
multiplicationF actor), Integer)
Dim processedBP As Bitmap = New Bitmap (processedWidth , processedHeight )
Dim g As Graphics = Graphics.FromIm age(processedBP )
Try
g.SmoothingMode = SmoothingMode.H ighQuality
g.Interpolation Mode = InterpolationMo de.HighQualityB icubic
g.PixelOffsetMo de = PixelOffsetMode .HighQuality
Dim rect As Rectangle = New Rectangle (0, 0, processedWidth,
processedHeight )
g.DrawImage(myI mage, rect, 0, 0, myImage.Width, myImage.Height,
GraphicsUnit.Pi xel)
processedBP.Rot ateFlip(RotateF lipType.Rotate1 80FlipNone)
processedBP.Rot ateFlip(RotateF lipType.Rotate1 80FlipNone)
processedBP.Sav e(processedMemS tream, ImageFormat.Jpe g)
Dim processedImageD ata As Byte = processedMemStr eam.ToArray()
If Not (processedImage Data Is Nothing) Then
context.Cache.A dd(cacheKey, processedImageD ata, Nothing,
Cache.NoAbsolut eExpiration, TimeSpan.FromMi nutes(10),
CacheItemPriori ty.Normal, New CacheItemRemove dCallback (Me.RemovedCall back))
context.Respons e.BinaryWrite(p rocessedImageDa ta)
processedImageD ata = Nothing
End If
Finally
g.Dispose()
processedBP.Dis pose()
End Try
Finally
processedMemStr eam.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************ *******@discuss ions.microsoft. com>
wrote in message news:0E******** *************** ***********@mic rosoft.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
4364
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 image in a byte of what is in the database. I also want to be able to display this image in an <asp:image></asp:image> control. Is there any...
6
2527
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 will them click the 'read more' link to view the entire article. It will also display a thumbnail of the article image. As the image is stored at its...
5
2380
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 = Graphics.FromImage( (Image) result ) ) { g1.DrawImage( b, 0, 0, nSize.Width, nSize.Height ); } working rather fine the first (and even second time), but each time...
3
3249
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 parent that holds my user control is resized, I resize my image so that it uses the maximum available space. Note: It takes about 2 seconds to...
2
9421
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 ratio * Provide white (or other color) borders at the sides or the top/bottom The last point would be used to allow users to resize the panel to any...
15
5312
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 path of the uploaded image, and resize it with the provided dimensions. My function is below. The current function is returning an error when run...
2
20889
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 would be appreciated. I've been trying numerous ways to resize images, as I want to make store thumbnails, not full images in my database. ...
8
9386
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 rotate the image by 90 deg. They are two differennt files i.e. resize.php and rotate.php. What I want to do is to combine both rotate.php &...
3
3569
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 little website: 1. Small: DOWNsize of original image to be used as a thumbnail. 2. Medium: DOWNsize of original image to be used as user...
22
4948
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 213 * 200.how can i make the image clear after i have resize it to 70 * 200.
0
7666
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7584
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7888
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8108
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7644
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6260
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5484
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
1
1201
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
925
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.