内存法遇到的问题
using System;using System.Collections.Generic;
using
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
namespace WindowsFormsApplication12
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
string curFile;
Bitmap curMap;
private void button1_Click(object sender, EventArgs e)
{
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
curFile = openFileDialog1.FileName;
curMap = (Bitmap)Image.FromFile(curFile);
}
Graphics g = this.CreateGraphics();
Rectangle rect = new Rectangle(50, 50, curMap.Width, curMap.Height);
g.DrawImage(curMap, rect);
}
private void button2_Click(object sender, EventArgs e)
{
Rectangle rect = new Rectangle(50,50, curMap.Width, curMap.Height);
System.Drawing.Imaging.BitmapData bmpData = curMap.LockBits(rect,System.Drawing.Imaging.ImageLockMode.ReadWrite,curMap.PixelFormat);//总是提示着有错误的呢
IntPtr ptr = bmpData.Scan0;
int bytes = bmpData.Stride * bmpData.Height;
byte[] rgbValues = new byte[bytes];
System.Runtime.InteropServices.Marshal.Copy(ptr, rgbValues, 0, bytes);
double colorTemp = 0;
for (int i = 0; i < bmpData.Height; i++)
{
for (int j = 0; j < bmpData.Width * 3; j += 3)
{
colorTemp = rgbValues[i * bmpData.Stride + j + 2] * 0.299 + rgbValues[i * bmpData.Stride + j + 1] * 0.587 + rgbValues[i * bmpData.Stride + j] * 0.114;
rgbValues[i * bmpData.Stride + j] = rgbValues[i * bmpData.Stride + j + 1] = rgbValues[i * bmpData.Stride + j + 2] = (byte)colorTemp;
System.Runtime.InteropServices.Marshal.Copy(rgbValues, 0, ptr, bytes);
curMap.UnlockBits(bmpData);
}
}
}
}
}