I had played with this image thing few months back , and in your case if you just want to resize your image you can look out the code below and hopefully it would help you out.
OK then you just need to read the image file into bytes and resize it and then save it.Below is the code
If ImgUpload.HasFile Then
Dim pFile As HttpPostedFile = ImgUpload.PostedFile
Dim iLen As Integer = pFile.ContentLength
Dim oStream As System.IO.Stream = pFile.InputStream
Dim bBuffer(iLen) As Byte
Dim iFile() As Byte
oStream.Read(bBuffer, 0, iLen)
iFile = bBuffer
bBuffer = ResizeImageFile(bBuffer, 45)
oStream.Close()
bBuffer = nothing
End If
Public Function ResizeImageFile(ByVal imageFile As Byte(), ByVal targetSize As Integer) As Byte()
Dim original As System.Drawing.Image = System.Drawing.Image.FromStream(New MemoryStream(imageFile))
Dim targetH As Integer, targetW As Integer
If original.Height > original.Width Then
targetH = targetSize
targetW = targetSize
'targetW = (int)(original.Width * ((float)targetSize / (float)original.Height));
Else
targetW = targetSize
targetH = targetSize
'targetH = (int)(original.Height * ((float)targetSize / (float)original.Width));
End If
Dim imgPhoto As System.Drawing.Image = System.Drawing.Image.FromStream(New MemoryStream(imageFile))
' Create a new blank canvas. The resized image will be drawn on this canvas.
Dim bmPhoto As New Bitmap(targetW, targetH, System.Drawing.Imaging.PixelFormat.Format24bppRgb)
bmPhoto.SetResolution(72, 72)
Dim grPhoto As Graphics = Graphics.FromImage(bmPhoto)
grPhoto.SmoothingMode = SmoothingMode.AntiAlias
grPhoto.InterpolationMode = InterpolationMode.HighQualityBicubic
grPhoto.PixelOffsetMode = PixelOffsetMode.HighQuality
grPhoto.DrawImage(imgPhoto, New Rectangle(0, 0, targetW, targetH), 0, 0, original.Width, original.Height, _
GraphicsUnit.Pixel)
' REMOVE COMMENTED CODE BELOW TO SAVE TO MEMORY
Dim mm As New MemoryStream()
bmPhoto.Save(mm, System.Drawing.Imaging.ImageFormat.Jpeg)
'''' SAVE TO FILE DIRECTLY FROM HERE
Dim sFileName As String
sFileName = GetYourFileNameWithFullPath()
bmPhoto.Save(sFileName, ImageFormat.Bmp)
'''''DISPOSE OF ALL OBJECT
original.Dispose()
imgPhoto.Dispose()
bmPhoto.Dispose()
grPhoto.Dispose()
'''''RETRUN THE BUUFER CONTENT
Return mm.GetBuffer()
End Function
Member
152 Points
886 Posts
To save a uploaded image in specific format
Jul 08, 2010 11:35 AM|geet|LINK
Hello Everyone,
How can one save an uploaded image in a specific dimension ie in a particular height, width without distorting actual image?It's very urgent.
Thanks
Participant
964 Points
319 Posts
Re: To save a uploaded image in specific format
Jul 08, 2010 12:44 PM|NiravVyas|LINK
HI Geet,
I had played with this image thing few months back , and in your case if you just want to resize your image you can look out the code below and hopefully it would help you out.
http://snippets.dzone.com/posts/show/1485
Thanks and Regards
Nirav Vyas
Please Mark As Answer If found useful
Thanks
Nirav
Member
300 Points
86 Posts
Re: To save a uploaded image in specific format
Jul 08, 2010 12:50 PM|rajum|LINK
Member
152 Points
886 Posts
Re: To save a uploaded image in specific format
Jul 08, 2010 01:59 PM|geet|LINK
Hi
Thanks for Reply. But I am storing Image like this:
if ((btnSave.Text == "Update" && FileUpload1.FileName != "") || btnSave.Text == "Save")
{
String sp;
sp = Server.MapPath("../UploadImg");
String fn = FileUpload1.FileName;
if (sp.EndsWith("//") == false)
{
sp += "//";
}
sp += fn;
FileUpload1.PostedFile.SaveAs(sp);
strFN = FileUpload1.FileName;
}
cmd.Parameters.Add("@img", SqlDbType.VarChar, 50).Value = strFN;
Participant
964 Points
319 Posts
Re: To save a uploaded image in specific format
Jul 09, 2010 07:16 AM|NiravVyas|LINK
OK then you just need to read the image file into bytes and resize it and then save it.Below is the code
Thanks and Regards
Nirav Vyas
Please Mark As Answer If found useful
Thanks
Nirav
Contributor
4392 Points
933 Posts
Re: To save a uploaded image in specific format
Jul 12, 2010 03:38 AM|Vipindas|LINK
using System.Drawing;
using System.Drawing.Drawing2D;
public void ResizeStream(int imageSize, Stream filePath, string outputPath)
{
var image = Image.FromStream(filePath);
int thumbnailSize = imageSize;
int newWidth, newHeight;
if (image.Width > image.Height)
{
newWidth = thumbnailSize;
newHeight = image.Height * thumbnailSize / image.Width;
}
else
{
newWidth = image.Width * thumbnailSize / image.Height;
newHeight = thumbnailSize;
}
var thumbnailBitmap = new Bitmap(newWidth, newHeight);
var thumbnailGraph = Graphics.FromImage(thumbnailBitmap);
thumbnailGraph.CompositingQuality = CompositingQuality.HighQuality;
thumbnailGraph.SmoothingMode = SmoothingMode.HighQuality;
thumbnailGraph.InterpolationMode = InterpolationMode.HighQualityBicubic;
var imageRectangle = new Rectangle(0, 0, newWidth, newHeight);
thumbnailGraph.DrawImage(image, imageRectangle);
thumbnailBitmap.Save(outputPath, image.RawFormat);
thumbnailGraph.Dispose();
thumbnailBitmap.Dispose();
image.Dispose();
}
then call resize image function as
ResizeImage(400, File1.FileContent, path);