简体   繁体   中英

Loading an image in C#

I 'm beginning C# programming and I want to make a simple program that loads an image and displays it in a window. Is there a standard way of doing it and/or a simplest way?

What I'm asking for is a counterpart (in simplicity and power) of Python's PIL module, a way of writing this

from PIL import Image, ImageFilter

try:
    orig = Image.open("img.jpg")
    orig.show()
except:
    print "Unable to load image"

in C# (preferably in a console application as I'm still exploring the language)

Here's a minimal console application example using WinForms:

using System.Windows.Forms;

public static void Main()
{   
    var image = new PictureBox();
    image.Dock = DockStyle.Fill;        
    image.Load(@"img.jpg");
    var f = new Form();
    f.Controls.Add(image);      
    Application.Run(f);
}

You'll have to add a project reference to System.Windows.Forms.dll; it won't be there by default in a console application.

It would be even easier of course, if you started with a Windows Forms project, used the form designer and dragged the picture box onto the form.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM