How to resize a image in PHP

To resize a image in PHP you can use the following snippet.

Sample PHP

function ImageResize($imageName, $tempName, $maxWidth, $maxHeight)
{
	$extension = explode(".", $imageName);
	$extension = $extension[count($extension)-1];
 
	if($extension == "jpeg" || $extension == "jpg")
	{
		$image = imagecreatefromjpeg($tempName);
	}
        elseif($extension == "gif")
	{
		$image = imagecreatefromgif($tempName);
	}
	elseif($extension == "png")
	{
		$image = imagecreatefrompng($tempName);
	}
	
	$Width = imagesx($image);
	$Height = imagesy($image);
 
	if($Width <= $maxWidth && $Height <= $maxHeight)
	{
		return $image;
	}
 
	if($Width >= $Height) 
	{
		$newWidth = $maxWidth;
		$newHeight = $newWidth * $Height / $Width;
	}
	else 
	{
		$newHeight = $maxHeight;
		$newWidth = $Width / $Height * $newHeight;
	}
 
	$image2 = imagecreatetruecolor($newWidth, $newHeight);
	imagecopyresampled($image2, $image, 0, 0, 0, 0, floor($newWidth), floor($newWidth), $Width, $Height);
	return $image2; 
}

How to take a screenshot in Java

To take a screenshot in Java you can use the following snippet.

Sample Java

BufferedImage bufferedImage = new Robot().createScreenCapture(new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()));
ImageIO.write(bufferedImage, "png", new File("C:\screenshot.png"));

for more informations see BufferedImage, ImageIO, Toolkit: getScreenSize(), Robot


How to download a image from url in C# and VB.NET

Sample C#

private static void DownloadImage(string url, string saveFilename)
{
	var httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
	var httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
	if ((httpWebResponse.StatusCode != HttpStatusCode.OK && 
		httpWebResponse.StatusCode != HttpStatusCode.Moved && 
		httpWebResponse.StatusCode != HttpStatusCode.Redirect) 
		|| !httpWebResponse.ContentType.StartsWith("image", StringComparison.OrdinalIgnoreCase))
	{
		return;
	}
	using (var stream = httpWebResponse.GetResponseStream())
	using (var fileStream = File.OpenWrite(saveFilename))
	{
		var bytes = new byte[4096];
		var read=0;
		do
		{
			if (stream == null) {continue;}
			read = stream.Read(bytes, 0, bytes.Length);
			fileStream.Write(bytes, 0, read);
		} while (read != 0);
	}
}

Sample VB.NET

Private Shared Sub DownloadImage(url As String, saveFilename As String)
	Dim httpWebRequest = DirectCast(WebRequest.Create(url), HttpWebRequest)
	Dim httpWebResponse = DirectCast(httpWebRequest.GetResponse(), HttpWebResponse)
	If (httpWebResponse.StatusCode <> HttpStatusCode.OK AndAlso httpWebResponse.StatusCode <> HttpStatusCode.Moved AndAlso httpWebResponse.StatusCode <> HttpStatusCode.Redirect) OrElse Not httpWebResponse.ContentType.StartsWith("image", StringComparison.OrdinalIgnoreCase) Then
		Return
	End If
	Using stream = httpWebResponse.GetResponseStream()
		Using fileStream = File.OpenWrite(saveFilename)
			Dim bytes = New Byte(4095) {}
			Dim read = 0
			Do
				If stream Is Nothing Then
					Continue Do
				End If
				read = stream.Read(bytes, 0, bytes.Length)
				fileStream.Write(bytes, 0, read)
			Loop While read <> 0
		End Using
	End Using
End Sub

How to load image from web asynchron into picturebox in C# and VB.NET

To load image from web asynchron into picturebox in C# and VB.NET you can use the following snippet.

Sample C#

pictureBox1.WaitOnLoad = false;
pictureBox1.LoadAsync(@"https://upload.wikimedia.org/wikipedia/commons/3/3f/Fronalpstock_big.jpg");

Sample VB.NET

pictureBox1.WaitOnLoad = False
pictureBox1.LoadAsync("https://upload.wikimedia.org/wikipedia/commons/3/3f/Fronalpstock_big.jpg")

How to convert a bitmap to icon in C# and VB.NET

To convert a bitmap to icon in C# and VB.NET you can use the following extension method.

Sample C#

public static Icon ToIcon(this Bitmap img, bool makeTransparent, Color colorToMakeTransparent)
{
	if (makeTransparent)
	{
		img.MakeTransparent(colorToMakeTransparent);
	}
	var iconHandle = img.GetHicon();
	return Icon.FromHandle(iconHandle);
}

Sample VB.NET

<System.Runtime.CompilerServices.Extension> _
Public Shared Function ToIcon(img As Bitmap, makeTransparent As Boolean, colorToMakeTransparent As Color) As Icon
	If makeTransparent Then
		img.MakeTransparent(colorToMakeTransparent)
	End If
	Dim iconHandle = img.GetHicon()
	Return Icon.FromHandle(iconHandle)
End Function

How to resize a image using C# or VB.NET

To resize a image using C# or VB.NET you can use the following snippet.

Sample C#

public static Image ResizeImage(Image img, int width, int height)
{
    var newImage = new Bitmap(width, height);
    using (var gr = Graphics.FromImage(newImage))
    {
       gr.SmoothingMode = SmoothingMode.HighQuality;
       gr.InterpolationMode = InterpolationMode.HighQualityBicubic;
       gr.PixelOffsetMode = PixelOffsetMode.HighQuality;
       gr.DrawImage(img, new Rectangle(0, 0, width, height));
     }
     return newImage;
}

public static Image ResizeImage(Image img, Size size)
{
	return ResizeImage(img, size.Width, size.Height);
}

public static Image ResizeImage(Bitmap bmp, int width, int height)
{
	return ResizeImage((Image)bmp, width, height);
}

public static Image ResizeImage(Bitmap bmp, Size size)
{
	return ResizeImage((Image)bmp, size.Width, size.Height);
}

Sample VB.NET

Public Shared Function ResizeImage(img As Image, width As Integer, height As Integer) As Image
	Dim newImage = New Bitmap(width, height)
	Using gr = Graphics.FromImage(newImage)
		gr.SmoothingMode = SmoothingMode.HighQuality
		gr.InterpolationMode = InterpolationMode.HighQualityBicubic
		gr.PixelOffsetMode = PixelOffsetMode.HighQuality
		gr.DrawImage(img, New Rectangle(0, 0, width, height))
	End Using
	Return newImage
End Function

Public Shared Function ResizeImage(img As Image, size As Size) As Image
	Return ResizeImage(img, size.Width, size.Height)
End Function

Public Shared Function ResizeImage(bmp As Bitmap, width As Integer, height As Integer) As Image
	Return ResizeImage(DirectCast(bmp, Image), width, height)
End Function

Public Shared Function ResizeImage(bmp As Bitmap, size As Size) As Image
	Return ResizeImage(DirectCast(bmp, Image), size.Width, size.Height)
End Function

Here are the same methods as Extensions Methods

Sample C#

public static Image ResizeImage(this Image img, int width, int height)
{
    var newImage = new Bitmap(width, height);
    using (var gr = Graphics.FromImage(newImage))
    {
       gr.SmoothingMode = SmoothingMode.HighQuality;
       gr.InterpolationMode = InterpolationMode.HighQualityBicubic;
       gr.PixelOffsetMode = PixelOffsetMode.HighQuality;
       gr.DrawImage(img, new Rectangle(0, 0, width, height));
     }
     return newImage;
}

public static Image ResizeImage(this Image img, Size size)
{
	return ResizeImage(img, size.Width, size.Height);
}

public static Image ResizeImage(this Bitmap bmp, int width, int height)
{
	return ResizeImage((Image)bmp, width, height);
}

public static Image ResizeImage(this Bitmap bmp, Size size)
{
	return ResizeImage((Image)bmp, size.Width, size.Height);
}

Sample VB.NET

<System.Runtime.CompilerServices.Extension> _
Public Shared Function ResizeImage(img As Image, width As Integer, height As Integer) As Image
	Dim newImage = New Bitmap(width, height)
	Using gr = Graphics.FromImage(newImage)
		gr.SmoothingMode = SmoothingMode.HighQuality
		gr.InterpolationMode = InterpolationMode.HighQualityBicubic
		gr.PixelOffsetMode = PixelOffsetMode.HighQuality
		gr.DrawImage(img, New Rectangle(0, 0, width, height))
	End Using
	Return newImage
End Function

<System.Runtime.CompilerServices.Extension> _
Public Shared Function ResizeImage(img As Image, size As Size) As Image
	Return ResizeImage(img, size.Width, size.Height)
End Function

<System.Runtime.CompilerServices.Extension> _
Public Shared Function ResizeImage(bmp As Bitmap, width As Integer, height As Integer) As Image
	Return ResizeImage(DirectCast(bmp, Image), width, height)
End Function

<System.Runtime.CompilerServices.Extension> _
Public Shared Function ResizeImage(bmp As Bitmap, size As Size) As Image
	Return ResizeImage(DirectCast(bmp, Image), size.Width, size.Height)
End Function