Last post Jun 10, 2008 06:08 AM by Trickass
Jun 10, 2008 04:34 AM|Trickass|LINK
I'm having some trouble with resizing an image, which I should upload to FTP.
The uploading part works fine, but the resizing doesn't seem to do anything :(
If any of you guys would be able to help, it would be great :)
Here's the code I've been using ...
public static Stream GenerateThumbnail(Stream _stream, int _height, int _width)
Image img = Image.FromStream(_stream);
Image thumb = new Bitmap(_width, _height, img.PixelFormat);
Graphics g = Graphics.FromImage(thumb);
g.CompositingQuality = CompositingQuality.HighQuality;
g.SmoothingMode = SmootingMode.HighQuality;
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
Rectangle rect = new Rectangle(0, 0, _width, _height);
public static string UploadFile(Stream _fileStream, string _fileName)
Stream thumbStream = GenerateThumbnail(_fileStream, 50, 50);
string fullPath = Path.Combine("ftp://localhost/", _filename);
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(fullPath);
request.Credentials = new NetwordCredential("user", "pass");
request.ContentLength = thumbStream.Length;
request.UseBinary = true;
request.Method = WebRequestMethods.Ftp.UploadFile;
Stream requestStream = request.GetRequestStream();
int i = 0;
byte buffer = new byte;
thumbStream.Position = 0;
while ((i = thumbStream.Read(buffer, 0, buffer.Length)) > 0)
requestStream.Write(buffer, 0, i);
FtpWebResponse response = request.GetResponse() as FtpWebResponse;
Jun 10, 2008 05:17 AM|ilivewithian|LINK
The thumbnail code I'm using is in VB, it works and you're welcome to translate it:
Private Sub SaveImageThumbnail(ByVal x As Integer, ByVal y As Integer, ByVal width As Integer, ByVal height As Integer)
' the following few lines seems odd, but essentially we are converting
' all images into a jpeg, that way we don't have to worry about indexed
' file formats which cause problems with GDI
Using memStr As New IO.MemoryStream()
Using imgDonor As Drawing.Bitmap = CType(Bitmap.FromFile(document.DocURL, False), Bitmap)
Using imag As Drawing.Image = Drawing.Image.FromStream(memStr)
Using img As Bitmap = New Bitmap(New Bitmap(imag))
Using targetImg As New Bitmap(imageSize, imageSize)
Using gfx As Graphics = Graphics.FromImage(targetImg)
' We could use image.GetThumbnailImage, but this creates low quality
' images, which look grainy and generally pap, so ...
gfx.SmoothingMode = Drawing2D.SmoothingMode.HighQuality
gfx.InterpolationMode = Drawing2D.InterpolationMode.High
Dim destRect As New Rectangle(0, 0, imageSize, imageSize)
gfx.DrawImage(img, destRect, x, y, width, height, GraphicsUnit.Pixel)
targetImg.Save(document.DocURL + "_thumb.jpg", ImageFormat.Jpeg)
Jun 10, 2008 05:34 AM|Trickass|LINK
Oke, this works great :)
Only one question though ... this code generates a thumbnail, so actually crops the image for a certain part ...
Now, is it possible to really resize the image ? So that the entire image is shown, just smaller?
Jun 10, 2008 06:01 AM|ilivewithian|LINK
Yep, easy, you'll need to rewrite a bit of the code, but essentially, get rid of the x an y values. Simply replace them with 0 and 0, take the width and height with the actual image width and height.
Then take the imageSize variable and replace it with the height and width that you want the images to be resized to.
Hope that helps.
Jun 10, 2008 06:08 AM|Trickass|LINK
Woaw that's just perfect !!! thanks a LOT ! :D