如何用C# 打开和保存BMP文件
思路:使用MenuStrip控件,当点击下拉菜单中的打开时,弹出打开对话框,点击另存为时,弹出另存为对话框做法:建立Form1窗体,pictureBox1图片框,menu...
思路:使用MenuStrip控件,当点击下拉菜单中的打开时,弹出打开对话框,点击另存为时,弹出另存为对话框
做法:建立Form1窗体,pictureBox1图片框,menuSrip1下拉列表控件
private void 打开图像ToolStripMenuItem_Click(object sender, EventArgs e)
{
Stream myStream;
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.InitialDirectory = "C:\\Documents and Settings\\Administrator\\My Documents\\My Pictures";
openFileDialog1.Filter = "BMP图形文件 (*.bmp)|*.bmp";
openFileDialog1.FilterIndex = 1;
openFileDialog1.RestoreDirectory = true;
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
if ((myStream = openFileDialog1.OpenFile()) != null)
{
pictureBox1.Image = new Bitmap(openFileDialog1.FileName);
/*至此可以打开图片,并显示在图片框控件当中,但是如果要对图片的二进制数据做进一步的操作,应该如何做?比如,打开的是一幅8位灰度图像,如何将它存入数组?*/
myStream.Close();
}
}
}
private void 另存为ToolStripMenuItem_Click(object sender, EventArgs e)
{
SaveFileDialog saveFileDialog1 = new SaveFileDialog();
saveFileDialog1.InitialDirectory = "C:\\Documents and Settings\\Administrator\\My Documents\\My Pictures";
saveFileDialog1.Filter = "Bitmap Image|*.bmp";
saveFileDialog1.Title = "保存图像";
saveFileDialog1.ShowDialog();
if(saveFileDialog1.FileName != "")
{
{
/*点击另存为按钮会引发对话框,但是不知道如何实际存入文件系统,此处应该写什么代码才能将图片存入磁盘内?*/
}
}
private void 保存图像ToolStripMenuItem_Click(object sender, EventArgs e)
{
//这里如何才能只点击保存按钮,就把当前的图像直接保存?
} 展开
做法:建立Form1窗体,pictureBox1图片框,menuSrip1下拉列表控件
private void 打开图像ToolStripMenuItem_Click(object sender, EventArgs e)
{
Stream myStream;
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.InitialDirectory = "C:\\Documents and Settings\\Administrator\\My Documents\\My Pictures";
openFileDialog1.Filter = "BMP图形文件 (*.bmp)|*.bmp";
openFileDialog1.FilterIndex = 1;
openFileDialog1.RestoreDirectory = true;
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
if ((myStream = openFileDialog1.OpenFile()) != null)
{
pictureBox1.Image = new Bitmap(openFileDialog1.FileName);
/*至此可以打开图片,并显示在图片框控件当中,但是如果要对图片的二进制数据做进一步的操作,应该如何做?比如,打开的是一幅8位灰度图像,如何将它存入数组?*/
myStream.Close();
}
}
}
private void 另存为ToolStripMenuItem_Click(object sender, EventArgs e)
{
SaveFileDialog saveFileDialog1 = new SaveFileDialog();
saveFileDialog1.InitialDirectory = "C:\\Documents and Settings\\Administrator\\My Documents\\My Pictures";
saveFileDialog1.Filter = "Bitmap Image|*.bmp";
saveFileDialog1.Title = "保存图像";
saveFileDialog1.ShowDialog();
if(saveFileDialog1.FileName != "")
{
{
/*点击另存为按钮会引发对话框,但是不知道如何实际存入文件系统,此处应该写什么代码才能将图片存入磁盘内?*/
}
}
private void 保存图像ToolStripMenuItem_Click(object sender, EventArgs e)
{
//这里如何才能只点击保存按钮,就把当前的图像直接保存?
} 展开
2个回答
展开全部
操作如下:
Bitmap _Bitmap = (Bitmap)Image.FromFile(@"c:\1.BMP");
BitmapData _BitmapData = _Bitmap.LockBits(new Rectangle(0, 0, _Bitmap.Width, _Bitmap.Height), ImageLockMode.ReadWrite, _Bitmap.PixelFormat);
byte[] _Value = new byte[_BitmapData.Stride * _BitmapData.Height];
Marshal.Copy(_BitmapData.Scan0, _Value, 0, _Value.Length);
写回去还是用 Marshal.Copy.
Bitmap _Bitmap = (Bitmap)Image.FromFile(@"c:\1.BMP");
BitmapData _BitmapData = _Bitmap.LockBits(new Rectangle(0, 0, _Bitmap.Width, _Bitmap.Height), ImageLockMode.ReadWrite, _Bitmap.PixelFormat);
byte[] _Value = new byte[_BitmapData.Stride * _BitmapData.Height];
Marshal.Copy(_BitmapData.Scan0, _Value, 0, _Value.Length);
写回去还是用 Marshal.Copy.
展开全部
image to byte[]
Image photo = new Bitmap(selectPictureFile);
System.IO.MemoryStream ms = new System.IO.MemoryStream();
photo.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
byte[] imagedata = ms.GetBuffer();
byte[] to image
System.IO.MemoryStream ms = New IO.MemoryStream(imagedata)
Bitmap b = new Bitmap(ms);
后面的 保存
pictureBox2.Image.Save(saveFileDialog1.FileName , System.Drawing.Imaging.ImageFormat.Bmp);
至于你后面的直接保存,就留下前面的saveFileDialog1.FileName,然后
pictureBox2.Image.Save(保存的变量, System.Drawing.Imaging.ImageFormat.Bmp);
完了
Image photo = new Bitmap(selectPictureFile);
System.IO.MemoryStream ms = new System.IO.MemoryStream();
photo.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
byte[] imagedata = ms.GetBuffer();
byte[] to image
System.IO.MemoryStream ms = New IO.MemoryStream(imagedata)
Bitmap b = new Bitmap(ms);
后面的 保存
pictureBox2.Image.Save(saveFileDialog1.FileName , System.Drawing.Imaging.ImageFormat.Bmp);
至于你后面的直接保存,就留下前面的saveFileDialog1.FileName,然后
pictureBox2.Image.Save(保存的变量, System.Drawing.Imaging.ImageFormat.Bmp);
完了
参考资料: http://hi.baidu.com/donghaozheng/blog/item/91bf0c08fd60f4d463d9863b.html
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |