c# 调用floodfill的问题

lyx725 2011-10-24 11:15:29
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using System.IO;
using System.Collections;
using System.Runtime.InteropServices; //*********FloodFIll

using System.Drawing.Drawing2D;

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

[DllImport("gdi32 ")] //*********FloodFIll
static extern int FloodFill(int hdc, int x, int y, int crColor); //*********FloodFIll

#region Variables
Bitmap iniImg;
Graphics graphics;
#endregion

private void NewImage(Bitmap bmp)
{
iniImg = bmp;
Bitmap changedImg = (Bitmap)iniImg.Clone();
pictureBox1.Image = changedImg;
graphics = Graphics.FromImage(changedImg);
}

private void Form1_Load(object sender, EventArgs e)
{
Bitmap bmp = new Bitmap(pictureBox1.Width, pictureBox1.Height);
Graphics dc = Graphics.FromImage(bmp);

//ReDrawAll rda = new ReDrawAll();

//rda.ReDrawAllPoints(dc);

char sep1 = ' ';
string sLine1;
string[] sTemp1;
double Longitude1, Latitude1;
//int x1, y1;
StreamReader sr1 = new StreamReader("县界图.txt", Encoding.Default);

while ((sLine1 = sr1.ReadLine()) != null)
{
string[] sArray = sLine1.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries);
string temp = sArray[1];
if (temp != "map")
{
sTemp1 = sLine1.Split(sep1);

int[] x1 = new int[sTemp1.Length / 2];
int[] y1 = new int[sTemp1.Length / 2];
Point[] point = new Point[sTemp1.Length / 2];
for (int i = 0; i < sTemp1.Length / 2; i++)
{
Longitude1 = Convert.ToDouble(sTemp1[i * 2]);
Latitude1 = Convert.ToDouble(sTemp1[i * 2 + 1]);
x1[i] = (int)(530 / 4.702 * (Longitude1 - 113.548) + 30);
y1[i] = (int)(-530 / 4.128 * (Latitude1 - 30.655) - 40);
point[i] = new Point(x1[i], y1[i]);
}

Pen pen = new Pen(Color.FromArgb(0, 0, 0), 1);//********************边界为黑色
pen.DashStyle = System.Drawing.Drawing2D.DashStyle.Solid;

for (int i = 0; i < (sTemp1.Length / 2) - 1; i++)
{
Point[] curvePoints = { point[i], point[i + 1] };
dc.DrawCurve(pen, curvePoints, 0.4f);
}
}
}
sr1.Close();


//填色
char sep = ' ';
string sLine;
string[] sTemp;
double Longitude, Latitude;
int x, y;
StreamReader sr;
string StationName;

sr = new StreamReader("站点.txt", Encoding.Default);
while ((sLine = sr.ReadLine()) != null)
{
sTemp = sLine.Split(sep);
StationName = sTemp[1];
Longitude = Convert.ToDouble(sTemp[2]);
Latitude = Convert.ToDouble(sTemp[3]);
x = (int)(-530 / 4.128 * (Latitude - 30.655)) - 50;//-是为了让图像向上提一些
y = (int)(530 / 4.702 * (Longitude - 113.548)) + 20;

//Color c = Color.FromArgb(0, 0, 0);
int cr = 0;
//dc.DrawString(StationName, new Font("微软雅黑", 9f), new SolidBrush(Color.Black), new PointF(y, x));
dc.FillRectangle(new SolidBrush(Color.Red), x, y, 1, 1);//填充由一对坐标、一个宽度和一个高度指定的矩形的内部
FloodFill(Convert.ToInt32(pictureBox1.Handle.ToString()), x, y, cr);
}
sr.Close();

NewImage(bmp); dc.Dispose();
}
}
}


想通过每个县某点的经纬度填充整个县颜色,但floodfill感觉没反应,求教
...全文
227 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
lyx725 2011-10-24
  • 打赏
  • 举报
回复
还是不行,graphics哪个功能可以替代floodfill,能给个连接么
另外,我这句会不会有问题dc.FillRectangle(new SolidBrush(Color.Red), x, y, 1, 1);//
县界边界我设定的黑色Pen pen = new Pen(Color.FromArgb(0, 0, 0), 1);//
floodfill的crColor我设的0,不知道是不是黑色
sdl2005lyx 2011-10-24
  • 打赏
  • 举报
回复
hdc是设备句柄,对应的是Graphics !你把那句改成:

FloodFill(Convert.ToInt32(dc.GetHdc(), x, y, cr);

或者:

FloodFill(pictureBox1.Handle.ToInt32(), x, y, cr);


另外:不知道你为什么要调用这个API的函数,GDI+的Graphics完全可以替代这个功能,看看MSDN的介绍。
山东蓝鸟贵薪 2011-10-24
  • 打赏
  • 举报
回复
学习学习
sdl2005lyx 2011-10-24
  • 打赏
  • 举报
回复
试试效果,你就知道了!Graphics.FillXXX系列的填充方法完全可以取代api的函数。。。

110,571

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧