可以吧,不过要用unsafe模式但是,这里面你的理解似乎有偏差。〉〉bmp = System.Drawing.Image.FromHbitmap(hBitmap); 《------
〉〉《----箭头所指处,将hBitmap指向的内存保存为位图。
〉〉我的问题是:可否不把这块内存保存,而是直接对这块内存里的数据进行操作?事实上,bmp本身就是一块内存里的对象。并不是“从保存的文件”给你一个我的从迷宫图形生成路径的文件。
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;namespace GDIPlusImaging
{
/// <summary>
/// Summary description for WebForm1.
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Label Label1;
protected System.Web.UI.WebControls.ImageButton ImageButton3;
protected System.Web.UI.WebControls.Button Button1; private const string strFileName = "\\VSMagazine.gif";
private const string strFileNameMaze = "\\Maze.bmp";
private const string strFileNameMazeNow = "\\MazeNow.bmp";
protected System.Web.UI.WebControls.Label Label2;
protected System.Web.UI.WebControls.Button Button2;

private Bitmap bmpNew;
private int intRatio = 1;
private int intFx, intFy;
protected System.Web.UI.WebControls.Button ButtonZoomOut;
protected System.Web.UI.WebControls.Button ButtonZoomIn; private void Button1_Click(object sender, System.EventArgs e)
{
if (bmpNew != null)
bmpNew.Dispose (); int iTurn, iCount;
int iWidth, iHeight; iTurn = 0; iCount = 1;
Bitmap bmpTmp = (Bitmap)System.Drawing.Image.FromFile(Request.PhysicalApplicationPath
+ strFileNameMaze);
iWidth = bmpTmp.Width;
iHeight = bmpTmp.Height; bmpNew = new Bitmap (bmpTmp.Width, bmpTmp.Height);
Graphics g = Graphics.FromImage (bmpNew);
g.DrawImage (bmpTmp, 0,0);
Label1.Text = iWidth.ToString() + iHeight.ToString();
// Modify each pixel in the image:
Rectangle rc = new Rectangle (0,0,bmpNew.Width, bmpNew.Height);
BitmapData d = bmpNew.LockBits (rc, ImageLockMode.ReadWrite, bmpNew.PixelFormat); while (iCount>0)
{ unsafe 
{
int iRow, iCol;
int* p = (int*)d.Scan0;
int numPixels = d.Width * d.Height;
int[,] q = new int[d.Height,d.Width]; for(iRow=0;iRow<d.Height;iRow++)
for(iCol=0;iCol<d.Width;iCol++)
{
if(((*p)&(0x00ffffff))!=0)
q[iRow, iCol] = 1;
else
q[iRow, iCol] = 0;
p++;
} iCount = 0;
for(iRow=1;iRow<d.Height-1;iRow++)
for(iCol=1;iCol<d.Width-1;iCol++)
if(q[iRow, iCol] == 1)
{
if(q[iRow-1, iCol] +q[iRow+1, iCol]+q[iRow, iCol-1]+q[iRow, iCol+1]<= 1) 
{
q[iRow, iCol] = 0;
iCount ++;
}
} p = (int*)d.Scan0;
for(iRow=0;iRow<d.Height;iRow++)
for(iCol=0;iCol<d.Width;iCol++)
{
if(q[iRow, iCol] == 1)
*p = Color.White.ToArgb();
else
*p = Color.Black.ToArgb();
p++;
}
}
iTurn ++;
}

// Done, unlock:
bmpNew.UnlockBits (d);
bmpTmp.Dispose ();
g.Dispose (); bmpNew.Save(Request.PhysicalApplicationPath + strFileNameMazeNow,System.Drawing.Imaging.ImageFormat.Bmp);

Label1.Text = iTurn.ToString();
ImageButton3.ImageUrl = Request.ApplicationPath + strFileNameMazeNow;
ImageButton3.Visible = true;
} private void Page_Load(object sender, System.EventArgs e)
{
ImageButton3.ImageUrl = Request.ApplicationPath + strFileNameMaze;
} #region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{    
this.ImageButton3.Click += new System.Web.UI.ImageClickEventHandler(this.ImageButton3_Click);
this.Button1.Click += new System.EventHandler(this.Button1_Click);
this.Button2.Click += new System.EventHandler(this.Button2_Click);
this.ButtonZoomOut.Click += new System.EventHandler(this.ButtonZoomOut_Click);
this.ButtonZoomIn.Click += new System.EventHandler(this.ButtonZoomIn_Click);
this.Load += new System.EventHandler(this.Page_Load); }
#endregion public bool CallBackMethod()
{
return false;
} private void Button2_Click(object sender, System.EventArgs e)
{
if (bmpNew != null)
bmpNew.Dispose (); int iTurn, iCount;
int iWidth, iHeight; iTurn = 0; iCount = 1;
Bitmap bmpTmp = (Bitmap)System.Drawing.Image.FromFile(Request.PhysicalApplicationPath
+ strFileNameMaze);
iWidth = bmpTmp.Width;
iHeight = bmpTmp.Height; bmpNew = new Bitmap (bmpTmp.Width, bmpTmp.Height);
Graphics g = Graphics.FromImage (bmpNew);
g.DrawImage (bmpTmp, 0,0);
Label1.Text = iWidth.ToString() + iHeight.ToString();
// Modify each pixel in the image:
Rectangle rc = new Rectangle (0,0,bmpNew.Width, bmpNew.Height);
BitmapData d = bmpNew.LockBits (rc, ImageLockMode.ReadWrite, bmpNew.PixelFormat); while (iCount>0)
{ unsafe 
{
int iRow, iCol;
int* p = (int*)d.Scan0;
int numPixels = d.Width * d.Height;
int[,] q = new int[d.Height,d.Width]; for(iRow=0;iRow<d.Height;iRow++)
for(iCol=0;iCol<d.Width;iCol++)
{
if(((*p)&(0x00ffffff))!=0)
q[iRow, iCol] = 1;
else
q[iRow, iCol] = 0;
p++;
} iCount = 0;
for(iRow=1;iRow<d.Height-1;iRow++)
for(iCol=1;iCol<d.Width-1;iCol++)
if(q[iRow, iCol] == 1)
{
if(q[iRow-1, iCol] +q[iRow+1, iCol]+q[iRow, iCol-1]+q[iRow, iCol+1]<= 1) 
{
q[iRow, iCol] = 0;
iCount ++;
}
} p = (int*)d.Scan0;
for(iRow=0;iRow<d.Height;iRow++)
for(iCol=0;iCol<d.Width;iCol++)
{
if(q[iRow, iCol] == 1)
*p = Color.White.ToArgb();
else
*p = Color.Black.ToArgb();
p++;
}
}
iTurn ++;
}

// Done, unlock:
bmpNew.UnlockBits (d);
bmpTmp.Dispose ();
g.Dispose (); bmpNew.Save(Request.PhysicalApplicationPath + strFileNameMazeNow,System.Drawing.Imaging.ImageFormat.Bmp);

Label1.Text = iTurn.ToString();
ImageButton3.ImageUrl = Request.ApplicationPath + strFileNameMazeNow;
ImageButton3.Visible = true;
} private void ImageButton3_Click(object sender, System.Web.UI.ImageClickEventArgs e)
{
Label1.Text = e.X.ToString()+" "+e.Y.ToString();
intFx = e.X/intRatio;
intFy = e.Y/intRatio;
} private void ButtonZoomOut_Click(object sender, System.EventArgs e)
{
intRatio /= 2;
} private void ButtonZoomIn_Click(object sender, System.EventArgs e)
{
intRatio *=2;
ImageButton3.Width =3;
Bitmap bmpTmp = (Bitmap)System.Drawing.Image.FromFile(Request.PhysicalApplicationPath
+ strFileNameMaze);
iWidth = bmpTmp.Width;
iHeight = bmpTmp.Height;
} }
}