Skip to main content
infervour.com

Back to all posts

How to Create A Bitmap Via PowerShell?

Published on
5 min read
How to Create A Bitmap Via PowerShell? image

Best PowerShell Guides to Buy in October 2025

1 PowerShell for Sysadmins: Workflow Automation Made Easy

PowerShell for Sysadmins: Workflow Automation Made Easy

  • MASTER POWERSHELL FOR EFFICIENT WORKFLOW AUTOMATION.
  • EASY-TO-FOLLOW GUIDANCE TAILORED FOR SYSADMINS.
  • DURABLE PAPERBACK FOR LASTING REFERENCE ON THE JOB.
BUY & SAVE
$23.99 $39.99
Save 40%
PowerShell for Sysadmins: Workflow Automation Made Easy
2 Learn PowerShell in a Month of Lunches, Fourth Edition: Covers Windows, Linux, and macOS

Learn PowerShell in a Month of Lunches, Fourth Edition: Covers Windows, Linux, and macOS

BUY & SAVE
$39.99
Learn PowerShell in a Month of Lunches, Fourth Edition: Covers Windows, Linux, and macOS
3 Mastering PowerShell Scripting: Automate repetitive tasks and simplify complex administrative tasks using PowerShell

Mastering PowerShell Scripting: Automate repetitive tasks and simplify complex administrative tasks using PowerShell

BUY & SAVE
$26.62 $49.99
Save 47%
Mastering PowerShell Scripting: Automate repetitive tasks and simplify complex administrative tasks using PowerShell
4 PowerShell Cookbook: Your Complete Guide to Scripting the Ubiquitous Object-Based Shell

PowerShell Cookbook: Your Complete Guide to Scripting the Ubiquitous Object-Based Shell

BUY & SAVE
$49.49 $89.99
Save 45%
PowerShell Cookbook: Your Complete Guide to Scripting the Ubiquitous Object-Based Shell
5 PowerShell Pocket Reference: Portable Help for PowerShell Scripters

PowerShell Pocket Reference: Portable Help for PowerShell Scripters

BUY & SAVE
$18.99 $29.99
Save 37%
PowerShell Pocket Reference: Portable Help for PowerShell Scripters
6 PowerShell Automation and Scripting for Cybersecurity: Hacking and defense for red and blue teamers

PowerShell Automation and Scripting for Cybersecurity: Hacking and defense for red and blue teamers

BUY & SAVE
$32.60 $49.99
Save 35%
PowerShell Automation and Scripting for Cybersecurity: Hacking and defense for red and blue teamers
7 Scripting: Automation with Bash, PowerShell, and Python—Automate Everyday IT Tasks from Backups to Web Scraping in Just a Few Lines of Code (Rheinwerk Computing)

Scripting: Automation with Bash, PowerShell, and Python—Automate Everyday IT Tasks from Backups to Web Scraping in Just a Few Lines of Code (Rheinwerk Computing)

BUY & SAVE
$36.76 $49.95
Save 26%
Scripting: Automation with Bash, PowerShell, and Python—Automate Everyday IT Tasks from Backups to Web Scraping in Just a Few Lines of Code (Rheinwerk Computing)
8 Powershell for Beginners A Step-by-Step Guide to Learning Scripting, Cmdlets: Learn PowerShell Basics, Automate IT Tasks, and Boost Productivity with Clear Examples and Practical Exercises

Powershell for Beginners A Step-by-Step Guide to Learning Scripting, Cmdlets: Learn PowerShell Basics, Automate IT Tasks, and Boost Productivity with Clear Examples and Practical Exercises

BUY & SAVE
$24.99
Powershell for Beginners A Step-by-Step Guide to Learning Scripting, Cmdlets: Learn PowerShell Basics, Automate IT Tasks, and Boost Productivity with Clear Examples and Practical Exercises
9 Windows PowerShell in Action

Windows PowerShell in Action

  • UNOPENED BOX ENSURES PRISTINE CONDITION FOR CUSTOMER SATISFACTION.
  • INCLUDES ALL ESSENTIAL ACCESSORIES FOR IMMEDIATE USE AND CONVENIENCE.
  • FAST SHIPPING GUARANTEES QUICK DELIVERY TO ENHANCE CUSTOMER EXPERIENCE.
BUY & SAVE
$59.99
Windows PowerShell in Action
+
ONE MORE?

To create a bitmap image using PowerShell, you can follow these steps:

  1. Start by opening PowerShell, either by searching for it in the Start menu or by pressing Windows key + X and selecting "Windows PowerShell" or "Windows PowerShell (Admin)".
  2. Firstly, load the required assembly to work with System.Drawing namespace by running the following command: Add-Type -TypeDefinition @" using System; using System.Windows.Forms; using System.Drawing; public class BitmapCreator { public static void CreateBitmap() { // Your bitmap creation code goes here } } "@
  3. Next, you need to define the size and format of the bitmap. For example, to create a 400x300 pixel bitmap, you can write: $bitmap = New-Object System.Drawing.Bitmap(400, 300)
  4. Now, you can start drawing on the bitmap using the Graphics object: $graphics = [System.Drawing.Graphics]::FromImage($bitmap)
  5. To draw something on the bitmap, you can use various methods provided by the Graphics object, such as DrawRectangle, DrawLine, FillRectangle, etc. For example, to draw a red rectangle: $pen = New-Object System.Drawing.Pen "Red" $graphics.FillRectangle($pen.Brush, 100, 100, 200, 150)
  6. After you finish drawing on the bitmap, make sure to dispose of the Graphics object to free up system resources: $graphics.Dispose()
  7. Finally, you can save the bitmap to a file by specifying the file path and format. For example, to save the bitmap as a PNG image: $bitmap.Save("C:\path\to\save\image.png", [System.Drawing.Imaging.ImageFormat]::Png)
  8. Remember to dispose of the bitmap object once you are done to release the resources: $bitmap.Dispose()

These steps illustrate how to create a basic bitmap image using PowerShell. You can enhance it further by exploring more options and drawing techniques provided by the System.Drawing namespace.

How to set the background color of a bitmap image in PowerShell?

In order to set the background color of a bitmap image in PowerShell, you can utilize the System.Drawing namespace. Here's an example code snippet to help you out:

Add-Type -TypeDefinition @" using System; using System.Drawing; using System.Drawing.Imaging;

public class BitmapUtils
{
    public static void SetBackgroundColor(string imagePath, Color backgroundColor)
    {
        using (Bitmap bitmap = new Bitmap(imagePath))
        {
            using (Graphics graphics = Graphics.FromImage(bitmap))
            {
                graphics.Clear(backgroundColor);
            }

            bitmap.Save(imagePath, ImageFormat.Png);
        }
    }
}

"@

Usage example

$imagePath = "C:\path\to\image.bmp" $backgroundColor = "Red" # Any valid color name or RGB value is acceptable

[BitmapUtils]::SetBackgroundColor($imagePath, $backgroundColor)

Make sure to replace "C:\path\to\image.bmp" with the actual path to the bitmap image you want to modify. You can specify the desired background color using either a color name (e.g., "Red", "Green") or hexadecimal RGB value (e.g., "#FF0000" for red).

How to create a bitmap via PowerShell?

To create a bitmap image using PowerShell, you can utilize the .NET's System.Drawing namespace. Here's an example script to create a bitmap:

Add-Type -TypeDefinition @" using System; using System.Drawing;

public class BitmapCreator { public static void CreateBitmap(string outputPath) { Bitmap bitmap = new Bitmap(200, 200); // Specify the size of the bitmap image

    // Create a graphics object from the bitmap
    using (Graphics graphics = Graphics.FromImage(bitmap))
    {
        // Clear the bitmap with a white background
        graphics.Clear(Color.White);

        // Draw a rectangle on the bitmap
        graphics.DrawRectangle(new Pen(Color.Black, 2), 50, 50, 100, 100);

        // Draw some text on the bitmap
        using (Font font = new Font("Arial", 12))
        {
            graphics.DrawString("Hello, Bitmap!", font, Brushes.Black, 60, 120);
        }
    }

    bitmap.Save(outputPath);  // Save the bitmap to a file
    bitmap.Dispose();  // Dispose of the bitmap
}

} "@

Call the method to create the bitmap

[BitmapCreator]::CreateBitmap("C:\path\to\output\image.bmp")

In this example, a bitmap of size 200x200 pixels is created with a white background. A black rectangle and some text ("Hello, Bitmap!") are drawn on the bitmap using the Graphics object. Finally, the resulting bitmap is saved as an image file (e.g., image.bmp) at the specified output path.

Ensure that the outputPath parameter is set to your desired path and filename for the generated bitmap image.

What is the color depth of a bitmap image in PowerShell?

In PowerShell, the color depth of a bitmap image can be determined using the PixelFormat property of the System.Drawing.Bitmap class.

Here's an example of how you can retrieve the color depth of a bitmap image in PowerShell:

Add-Type -TypeDefinition @" using System; using System.Drawing;

public class BitmapInfo {
    public static int GetColorDepth(string filePath) {
        using (Bitmap bitmap = new Bitmap(filePath)) {
            return Image.GetPixelFormatSize(bitmap.PixelFormat);   
        }
    }
}

"@

$filePath = "C:\path\to\image.bmp" $colorDepth = [BitmapInfo]::GetColorDepth($filePath)

Write-Host "Color Depth: $colorDepth bits"

Replace "C:\path\to\image.bmp" with the actual path to your bitmap image. Running this PowerShell script will output the color depth in bits.

How to overlay one bitmap image onto another in PowerShell?

In PowerShell, you can overlay one bitmap image onto another by using the System.Drawing namespace and the .NET Framework's Graphics class. Here's an example script:

# Load the source and overlay images $sourceImage = [System.Drawing.Image]::FromFile("C:\path\to\source\image.png") $overlayImage = [System.Drawing.Image]::FromFile("C:\path\to\overlay\image.png")

Create a Bitmap object for the source image

$bitmap = New-Object System.Drawing.Bitmap($sourceImage.Width, $sourceImage.Height)

Create a Graphics object for the Bitmap

$graphic = [System.Drawing.Graphics]::FromImage($bitmap)

Draw the source image on the Bitmap

$graphic.DrawImage($sourceImage, 0, 0)

Calculate the position to overlay the image (e.g., bottom-right corner)

$x = $bitmap.Width - $overlayImage.Width - 10 $y = $bitmap.Height - $overlayImage.Height - 10

Draw the overlay image on the Bitmap

$graphic.DrawImage($overlayImage, $x, $y)

Save the resulting image

$bitmap.Save("C:\path\to\output\image.png")

Dispose the objects

$graphic.Dispose() $bitmap.Dispose() $sourceImage.Dispose() $overlayImage.Dispose()

Make sure to replace "C:\path\to\source\image.png" and "C:\path\to\overlay\image.png" with the actual paths to your source and overlay images. Adjust the position ($x and $y variables) as needed, based on where you want to overlay the image. Finally, specify the desired output path and filename in the Save method.