Here I am posting the sample code for resizing the pictures (thumb nail) on your web applications. Using this method we can resize the pictures with required width and height depends on application image size.
Calss.Vb file
--------------
Public Shared Function CreateThumbnail(ByVal [source] As Drawing.Bitmap, ByVal thumbWi As Integer, ByVal thumbHi As Integer) As Drawing.Bitmap
If [source].Width < thumbWi And [source].Height < thumbHi Then
Return [source]
End If
Dim ret As System.Drawing.Bitmap = Nothing
Try
Dim wi, hi As Integer
' this using image graphics interpolatin
'co-oridination technique
If [source].Width > [source].Height Then
wi = thumbWi
hi = CInt([source].Height * (CDec(thumbWi) / [source].Width))
Else
hi = thumbHi
wi = CInt([source].Width * (CDec(thumbHi) / [source].Height))
End If
' System.Drawing.Image ret = source.GetThumbnailImage(wi,hi,null,IntPtr.Zero);
ret = New Drawing.Bitmap(wi, hi)
Dim imagesize As String = Chr(110) + Chr(117)
Dim g As Drawing.Graphics = Drawing.Graphics.FromImage(ret)
Try
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic
g.FillRectangle(System.Drawing.Brushes.White, 0, 0, wi, hi)
g.DrawImage([source], 0, 0, wi, hi)
Finally
g.Dispose()
End Try
Catch
End Try
Return ret
End Function
Example.Aspx page
------------------
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Example.aspx.vb" Inherits="Example" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
</form>
</body>
</html>
Example.Aspx.vb page
--------------------
Imports System.Drawing
Imports System.Drawing.Imaging
Imports System.Drawing.Drawing2D
Imports System.Net
Partial Class Example
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim gImage As String = Request.QueryString("TestImg")
Dim imageUrl As String = "http://servername/foldername/" + Trim(gImage) + ".jpg"
Dim imageHeight As Integer = Request.QueryString("h")
Dim imageWidth As Integer = Request.QueryString("w")
Dim fullSizeImg As System.Drawing.Image
Dim c As System.Net.WebClient = New System.Net.WebClient
Try
c.UseDefaultCredentials = True
Dim s As IO.Stream = c.OpenRead(imageUrl)
fullSizeImg = System.Drawing.Image.FromStream(s)
s.Close()
c.Dispose()
c = Nothing
Catch ex As Exception
fullSizeImg = System.Drawing.Image.FromFile(Server.MapPath("NoPtImage .jpg"))
End Try
Response.ContentType = "image/jpeg"
Dim thumbNailImg As System.Drawing.Image
thumbNailImg = CreateThumbnail(fullSizeImg, imageHeight, imageWidth)
thumbNailImg.Save(Response.OutputStream, ImageFormat.Jpeg)
thumbNailImg.Dispose()
End Sub
End Class
ImgDisply.Aspx
------------
<asp:Table runat="server" CellPadding="0" CellSpacing="0" HorizontalAlign="center" Width="100%" >
<asp:TableRow>
<asp:TableCell> <asp:TextBox ID="ProductNum" Width="20%" runat="server"></asp:TextBox>
<asp:TableCell Width="70%" RowSpan="5" > <img alt="Pic" src="Example.aspx?TestImg=<%=ProductNum.Text %>&h=100&w=90" />
</asp:TableCell>
</asp:TableRow>
Thanks
Suji Ravi. m
Suji Ravi. M
MCAD