public
static
void MakeRemoteThumbnailImage(string url, string newFileName, int maxWidth, int maxHeight)
{
Stream stream = GetRemoteImage(url);
if
(stream == null)
return
;
Image original = Image.FromStream(stream);
stream.Close();
MakeThumbnailImage(original, newFileName, maxWidth, maxHeight);
}
private
static
Stream GetRemoteImage(string url)
{
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
request.Method =
"GET"
;
request.ContentLength = 0;
request.Timeout = 20000;
HttpWebResponse response = null;
try
{
response = (HttpWebResponse)request.GetResponse();
return
response.GetResponseStream();
}
catch
{
return
null;
}
}
public
static
bool MakeThumbnailImage(string fileName, string newFileName, int maxWidth, int maxHeight, int cropWidth, int cropHeight, int X, int Y)
{ byte[] imageBytes = File.ReadAllBytes(fileName);
Image originalImage = Image.FromStream(
new
System.IO.MemoryStream(imageBytes));
Bitmap b =
new
Bitmap(cropWidth, cropHeight);
try
{ using (Graphics g = Graphics.FromImage(b))
{
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.SmoothingMode = SmoothingMode.AntiAlias;
g.PixelOffsetMode = PixelOffsetMode.HighQuality;
g.DrawImage(originalImage,
new
Rectangle(0, 0, cropWidth, cropHeight), X, Y, cropWidth, cropHeight, GraphicsUnit.Pixel);
Image displayImage =
new
Bitmap(b, maxWidth, maxHeight);
SaveImage(displayImage, newFileName, GetCodecInfo(
"image/"
+ GetFormat(newFileName).ToString().ToLower()));
return
true;
}
}
catch
(System.Exception e)
{
throw
e;
} finally
{
originalImage.Dispose();
b.Dispose();
}
}