This function creates the Thumbnail image and returns the
image created in Byte() format
Private Function createThumnail()Function createThumnail(ByVal ImageStream As Stream,
ByVal tWidth As Double, ByVal tHeight As Double) As Byte()
Dim g As System.Drawing.Image =System.Drawing.Image.FromStream(ImageStream)
Dim thumbSize As New Size()
thumbSize =NewthumbSize(g.Width, g.Height, tWidth, tHeight)
Dim imgOutput As New Bitmap(g, thumbSize.Width, thumbSize.Height)
Dim imgStream As New MemoryStream()
Dim thisFormat = g.RawFormat
imgOutput.Save(imgStream, thisFormat)
Dim imgbin(imgStream.Length) As Byte
imgStream.Position = 0
Dim n As Int32 = imgStream.Read(imgbin, 0, imgbin.Length)
g.Dispose()
imgOutput.Dispose()
Return imgbin
End FunctionFunction NewthumbSize()Function NewthumbSize(ByVal currentwidth As Double, ByVal
currentheight As Double, ByVal newWidth As Double, ByVal newHeight As Double)
' Calculate the Size of the New image
Dim tempMultiplier As Double If currentheight > currentwidth Then ' portrait
tempMultiplier = newHeight / currentheight
Else
tempMultiplier = newWidth / currentwidth
End If Dim NewSize As New Size(CInt(currentwidth * tempMultiplier),
CInt(currentheight * tempMultiplier))
Return NewSize
End Function
image created in Byte() format
Private Function createThumnail()Function createThumnail(ByVal ImageStream As Stream,
ByVal tWidth As Double, ByVal tHeight As Double) As Byte()
Dim g As System.Drawing.Image =System.Drawing.Image.FromStream(ImageStream)
Dim thumbSize As New Size()
thumbSize =NewthumbSize(g.Width, g.Height, tWidth, tHeight)
Dim imgOutput As New Bitmap(g, thumbSize.Width, thumbSize.Height)
Dim imgStream As New MemoryStream()
Dim thisFormat = g.RawFormat
imgOutput.Save(imgStream, thisFormat)
Dim imgbin(imgStream.Length) As Byte
imgStream.Position = 0
Dim n As Int32 = imgStream.Read(imgbin, 0, imgbin.Length)
g.Dispose()
imgOutput.Dispose()
Return imgbin
End FunctionFunction NewthumbSize()Function NewthumbSize(ByVal currentwidth As Double, ByVal
currentheight As Double, ByVal newWidth As Double, ByVal newHeight As Double)
' Calculate the Size of the New image
Dim tempMultiplier As Double If currentheight > currentwidth Then ' portrait
tempMultiplier = newHeight / currentheight
Else
tempMultiplier = newWidth / currentwidth
End If Dim NewSize As New Size(CInt(currentwidth * tempMultiplier),
CInt(currentheight * tempMultiplier))
Return NewSize
End Function
源码在http://www.cnblogs.com/rippleyong/archive/2004/08/10/31977.aspx
我只要改变图片的大小的一些函数
{
System.Drawing.Image g = System.Drawing.Image.FromStream(ImageStream);
Size thumbSize = new Size();
thumbSize = NewthumbSize(g.Width, g.Height, tWidth, tHeight);
Bitmap imgOutput = new Bitmap(g, thumbSize.Width, thumbSize.Height);
MemoryStream imgStream = new MemoryStream();
object thisFormat = g.RawFormat;
imgOutput.Save(imgStream, thisFormat);
byte[imgStream.Length] imgbin;
imgStream.Position = 0;
Int32 n = imgStream.Read(imgbin, 0, imgbin.Length);
g.Dispose();
imgOutput.Dispose();
return imgbin;
} object NewthumbSize(double currentwidth, double currentheight, double newWidth, double newHeight)
{
double tempMultiplier;
if (currentheight > currentwidth) {
tempMultiplier = newHeight / currentheight;
} else {
tempMultiplier = newWidth / currentwidth;
}
Size NewSize = new Size(System.Convert.ToInt32(currentwidth * tempMultiplier), System.Convert.ToInt32(currentheight * tempMultiplier));
return NewSize;
}
private byte[] createThumnail(Stream ImageStream,Double tWidth,Double tHeight)
{
System.Drawing.Image g = System.Drawing.Image.FromStream(ImageStream);
System.Drawing.Size thumbSize = new Size(g.Width, g.Height, tWidth, tHeight);
System.Drawing.Bitmap imgOutput = new Bitmap(g,thumSize.Width,thumSize.Height);
System.IO.MemoryStream imgStream = new MemoryStream();
System.Drawing.Imaging.ImageFormat thisFormat = g.RawFormat;
imgOutput.Save(imgStream, thisFormat);
byte[] imgbin = new byte[imgStream.Length];
imgStream.Position = 0;
int n = imgStream.Read(imgbin,0,imgbin.Length);
g.Dispose();
imgOutput.Dispose();
return imgbin;
}public int NewthumbSize(Double currentwidth,Double currentheight,Double newWidth, Double newHeight)
{
Double tempMultiplier;
if(currentheight > currentwidth)
tempMultiplier = newHeight / currentheight;
else
tempMultiplier = newWidth / currentwidth;
System.Drawing.Size NewSize = new Size(Convert.ToInt32(currentwidth * tempMultiplier),Convert.ToInt32(currentheight * tempMultiplier));
return NewSize;
}
private byte[] createThumnail(Stream ImageStream,Double tWidth,Double tHeight)
{
System.Drawing.Image g = System.Drawing.Image.FromStream(ImageStream);
System.Drawing.Size thumbSize = new Size(g.Width, g.Height, tWidth, tHeight);
System.Drawing.Bitmap imgOutput = new Bitmap(g,thumSize.Width,thumSize.Height);
System.IO.MemoryStream imgStream = new MemoryStream();
System.Drawing.Imaging.ImageFormat thisFormat = g.RawFormat;
imgOutput.Save(imgStream, thisFormat);
byte[] imgbin = new byte[imgStream.Length];
imgStream.Position = 0;
int n = imgStream.Read(imgbin,0,imgbin.Length);
g.Dispose();
imgOutput.Dispose();
return imgbin;
}public int NewthumbSize(Double currentwidth,Double currentheight,Double newWidth, Double newHeight)
{
Double tempMultiplier;
if(currentheight > currentwidth)
tempMultiplier = newHeight / currentheight;
else
tempMultiplier = newWidth / currentwidth;
System.Drawing.Size NewSize = new Size(Convert.ToInt32(currentwidth * tempMultiplier),Convert.ToInt32(currentheight * tempMultiplier));
return NewSize;
}