在线等,如何在wpf中用后台代码新建一个图片,然后指定图片的位置并在界面显示?

2025-02-06 04:16:14
推荐回答(1个)
回答1:

private void ImageExampleForm_Paint(object sender, PaintEventArgs e)
{
// Create image.
Image newImage = Image.FromFile("SampImag.jpg");

// Create Point for upper-left corner of image.
Point ulCorner = new Point(100, 100);

// Draw image to screen.
e.Graphics.DrawImage(newImage, ulCorner);
}

Bitmap image1;

private void Button1_Click(System.Object sender, System.EventArgs e)
{

try
{
// Retrieve the image.
image1 = new Bitmap(@"C:\Documents and Settings\All Users\"
+ @"Documents\My Music\music.bmp", true);

int x, y;

// Loop through the images pixels to reset color.
for(x=0; x {
for(y=0; y {
Color pixelColor = image1.GetPixel(x, y);
Color newColor = Color.FromArgb(pixelColor.R, 0, 0);
image1.SetPixel(x, y, newColor);
}
}

// Set the PictureBox to display the image.
PictureBox1.Image = image1;

// Display the pixel format in Label1.
Label1.Text = "Pixel format: "+image1.PixelFormat.ToString();

}
catch(ArgumentException)
{
MessageBox.Show("There was an error." +
"Check the path to the image file.");
}
}