How to get a random Color in C# and VB.NET

To get a random Color in C# and VB.NET you can use the following snippet.

Sample C#

public static System.Drawing.Color RandomColor()
{
	var rand = new Random();
	var r = rand.Next(0, 256);
	Thread.Sleep(1);
	var g = rand.Next(0, 256);
	Thread.Sleep(1);
	var b = rand.Next(0, 256);
	return System.Drawing.Color.FromArgb(255, r, g, b);
}

Sample VB.NET

Public Shared Function RandomColor() As System.Drawing.Color
	Dim rand As var = New Random
	Dim r As var = rand.Next(0, 256)
	Thread.Sleep(1)
	Dim g As var = rand.Next(0, 256)
	Thread.Sleep(1)
	Dim b As var = rand.Next(0, 256)
	Return System.Drawing.Color.FromArgb(255, r, g, b)
End Function

How to rotate a image in C# and VB.NET

To rotate a image in C# and VB.NET you can use the following snippet.

Sample C#

public enum RotateMode
{
	Ninetee = 0,
	OneHundretEighty = 1,
	TwoHundredSeventy = 2,
}

public static Image RotateImage(Image myImage, RotateMode myRotatemode)
{
	var ret = myImage;
	switch (myRotatemode)
	{
		case RotateMode.Ninetee:
			ret.RotateFlip(RotateFlipType.Rotate90FlipNone);
			break;
		case RotateMode.OneHundretEighty:
			ret.RotateFlip(RotateFlipType.Rotate180FlipNone);
			break;
		case RotateMode.TwoHundredSeventy:
			ret.RotateFlip(RotateFlipType.Rotate270FlipNone);
			break;
	}
	return ret;
}

Sample VB.NET

Public Enum RotateMode
	Ninetee = 0
	OneHundretEighty = 1
	TwoHundredSeventy = 2
End Enum

Public Shared Function RotateImage(myImage As Image, myRotatemode As RotateMode) As Image
	Dim ret = myImage
	Select Case myRotatemode
		Case RotateMode.Ninetee
			ret.RotateFlip(RotateFlipType.Rotate90FlipNone)
			Exit Select
		Case RotateMode.OneHundretEighty
			ret.RotateFlip(RotateFlipType.Rotate180FlipNone)
			Exit Select
		Case RotateMode.TwoHundredSeventy
			ret.RotateFlip(RotateFlipType.Rotate270FlipNone)
			Exit Select
	End Select
	Return ret
End Function

How to flip a image in C# and VB.NET

To flip a image in C# and VB.NET you can use the following snippet.

Sample C#

public enum FlipMode
{
	Horizontal = 0,
	Vertical = 1,
	Both = 2
}

public Image FlipImage(Image myImage, FlipMode myFlipmode)
{
	var ret = myImage;
	switch (myFlipmode)
	{
		case FlipMode.Horizontal:
			ret.RotateFlip(RotateFlipType.RotateNoneFlipX);
			break;
		case FlipMode.Vertical:
			ret.RotateFlip(RotateFlipType.RotateNoneFlipY);
			break;
		case FlipMode.Both:
			ret.RotateFlip(RotateFlipType.RotateNoneFlipXY);
			break;
	}
	return ret;
}

Sample Vb.NET

Public Enum FlipMode
	Horizontal = 0
	Vertical = 1
	Both = 2
End Enum

Public Function FlipImage(myImage As Image, myFlipmode As FlipMode) As Image
	Dim ret = myImage
	Select Case myFlipmode
		Case FlipMode.Horizontal
			ret.RotateFlip(RotateFlipType.RotateNoneFlipX)
			Exit Select
		Case FlipMode.Vertical
			ret.RotateFlip(RotateFlipType.RotateNoneFlipY)
			Exit Select
		Case FlipMode.Both
			ret.RotateFlip(RotateFlipType.RotateNoneFlipXY)
			Exit Select
	End Select
	Return ret
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