본문 바로가기

.NET/GDI +

GDI + 비트맵 이미지 저장 관련 예제

반응형


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WindowsApplication54
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

Bitmap image1;
Bitmap image2;

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

try
{
// Retrieve the image.
image1 = new Bitmap(pictureBox1.Image);

int x, y;

// Loop through the images pixels to reset color.
for (x = 0; x < image1.Width; x++)
{
for (y = 0; y < image1.Height; y++)
{
Color pixelColor = image1.GetPixel(x, y);
Color newColor = Color.FromArgb(100, pixelColor.R, pixelColor.G, pixelColor.B);
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.");
}
}

private void Button5_Click(System.Object sender, System.EventArgs e)
{
image2.Save(@"c:\1.jpg");
try
{
if (image1 != null)
{
image1.Save("c:\\myBitmap.bmp");
button2.Text = "Saved file.";
label1.Text += "Saved file.";
}
}
catch (Exception)
{
MessageBox.Show("There was a problem saving the file." +
"Check the file permissions.");
}
}

private void button3_Click(object sender, EventArgs e)
{
try
{
image2 = new Bitmap(pictureBox1.Image, pictureBox1.Image.Width , pictureBox1.Image.Height );

int x, y;

// Loop through the images pixels to reset color.
for (x = 0; x < image2.Width; x++)
{
for (y = 0; y < image2.Height ; y++)
{
Color pixelColor = image2.GetPixel(x, y);
Color newColor = Color.FromArgb(255, pixelColor.R, pixelColor.G, pixelColor.B);
image2.SetPixel(x, y, newColor);
}
}

// Set the PictureBox to display the image.
pictureBox1.Image = image2;

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

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


}
}






'.NET > GDI +' 카테고리의 다른 글

GDI + 네이버 지식인 그림 저장하며 그리기 예제  (0) 2008.12.11
C# "GDI폴리곤";  (0) 2008.11.18
C# GDI32 GDI plus  (0) 2008.11.18
C# 리스트박스 개별 색깔넣기 GDI 사용  (0) 2008.11.18