C# 用c语言绘制图像#
我正在尝试在Windows窗体应用程序中绘制图像。我画了一些长方形而不是图片,但我想用一个特定的图像来替换它们。 我添加了整个代码,问题出在draw函数中。有人能帮忙吗C# 用c语言绘制图像#,c#,image,C#,Image,我正在尝试在Windows窗体应用程序中绘制图像。我画了一些长方形而不是图片,但我想用一个特定的图像来替换它们。 我添加了整个代码,问题出在draw函数中。有人能帮忙吗 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using Sys
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Threading;
using System.Drawing.Drawing2D;
using Microsoft.Win32;
namespace kiralyno
{
public partial class Form2 : Form
{
public static int p = 1;
public static int o = 1;
public float[] x1 = new float[80000];
public float[] y1 = new float[80000];
public string hely = " ";
public int db = 0;
public int[] v = new int[80000];
public static string a;
public string s = " ";
public static int n = Form1.n;
public float x = 0, y = 0, w = 450 / n, h = 450 / n;
public int helyes(int i, int k)
{
for (int j = 1; j <= k - 1; j++)
if ((v[j] == i) || (Math.Abs(k - j) == Math.Abs(i - v[j])))
return 0;
return 1;
}
public void kiir(int k)
{
for (int j = 1; j <= k; j++)
{
s += Convert.ToString(v[j]);
if (j != n) s += " ";
}
a = s;
megoldas.Items.Add(a);
s = "";
a = "";
}
public void back(int k)
{
for (int i = 1; i <= n; i++)
{
if (helyes(i, k) == 1)
{
v[k] = i;
if (k == n)
{
kiir(k);
draw();
Thread.Sleep(500);
sakktabla(0, 0, w, h);
db++;
}
else back(k + 1);
}
}
}
public void draw()
{
// Image newImage = Image.FromFile("D:/Akos/Visual/Projects/kiralyno/kir.png");
Graphics g = panel1.CreateGraphics();
for (int m = 1; m <= n; m++)
{
g.DrawRectangle(new Pen(Brushes.Blue), x1[v[m]], y1[m], w, h);
g.FillRectangle(Brushes.Blue, x1[v[m]], y1[m], w, h);
Thread.Sleep(200);
}
}
public Form2()
{
InitializeComponent();
}
public void sakktabla(float x, float y, float w, float h)
{
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= n; j++)
{
if ((i + j) % 2 == 0)
{
Graphics g = panel1.CreateGraphics();
g.DrawRectangle(new Pen(Brushes.Black), x, y, w, h);
g.FillRectangle(Brushes.Black, x, y, w, h);
x1[p] = x;
x = x + 450 / n;
}
else
{
Graphics g = panel1.CreateGraphics();
g.DrawRectangle(new Pen(Brushes.White), x, y, w, h);
g.FillRectangle(Brushes.White, x, y, w, h);
x1[p] = x;
x = x + 450 / n;
}
p++;
}
y1[o] = y;
for (int m = 1; m <= n; m++)
{
y1[n * m + o] = y1[o];
}
o++;
y = y + 450 / n;
x = 0;
}
}
private void panel1_Paint(object sender, PaintEventArgs e)
{
sakktabla(0, 0, w, h);
}
private void Form2_Deactivate(object sender, EventArgs e)
{
Application.Exit();
}
private void megoldas_Click(object sender, EventArgs e)
{
label1.Text = "";
back(1);
if (db == 0) megoldas.Items.Add("Nincs megoldas!");
string c = Convert.ToString(db);
megoldas.Items.Add(c+" db megoldas van");
megoldas.Enabled = false;
}
private void label1_Click(object sender, EventArgs e)
{
label1.Text = "";
back(1);
if (db == 0) megoldas.Items.Add("Nincs megoldas!");
string c = Convert.ToString(db);
megoldas.Items.Add(c + " db megoldas van");
megoldas.Enabled = false;
}
private void pictureBox1_Click(object sender, EventArgs e)
{
}
}
}
使用系统;
使用System.Collections.Generic;
使用系统组件模型;
使用系统数据;
使用系统图;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用System.Windows.Forms;
使用系统线程;
使用System.Drawing.Drawing2D;
使用Microsoft.Win32;
基拉利诺
{
公共部分类表单2:表单
{
公共静态int p=1;
公共静态INTO=1;
公共浮动[]x1=新浮动[80000];
公共浮动[]y1=新浮动[80000];
公共字符串hely=“”;
公共int db=0;
公共整数[]v=新整数[80000];
公共静态字符串a;
公共字符串s=“”;
公共静态int n=Form1.n;
公共浮动x=0,y=0,w=450/n,h=450/n;
公共国际机场(国际机场i、国际机场k)
{
对于(int j=1;j如果您确实需要自己绘制,请使用:
一般来说,使用a通常更容易。来源:
用什么替换它们?你不太清楚你期望的是什么。这里还有变量n,x1,y1,w,h,它们是什么并不明显。避免CreateGraphics。改用paint事件。你不需要新笔,只需要使用Pens.Blue。使用计时器而不是Sleep,这会阻塞。我更换了新笔,它工作正常hanks!我还上传了全部代码,以使所有内容都清晰明了。恐怕添加所有代码并不能使问题更清楚。我们需要明确知道您的问题是什么以及您试图实现什么。如果我使用PictureBox,如何在某些特定点上绘制多幅图像?您可以将PictureBox移动到您想要使用的位置成员的位置和大小。对于多张图片,请使用多个图片框。谢谢,这对我帮助很大!没问题。当我清楚地理解您的问题时,我不确定您为什么排名下降。
private void DrawImageRect(PaintEventArgs e)
{
// Create image.
Image newImage = Image.FromFile("SampImag.jpg");
// Create rectangle for displaying image.
Rectangle destRect = new Rectangle(100, 100, 450, 150);
// Draw image to screen.
e.Graphics.DrawImage(newImage, destRect);
}