我想在PictureBox中的图像上绘制网格。
但是,绘制网格时,图像消失了。
如何解决此代码?选中CheckBox或使用TrackBar更改网格大小时,它将绘制网格。
private void grid()
{
int x, y;
int w = pictureBox1.Size.Width;
int h = pictureBox1.Size.Height;
int inc = trackBar1.Value;
BackImage = new Bitmap(w, h);
Pen myPen = new Pen(Color.Green);
Graphics gr = Graphics.FromImage(BackImage);
gr.Clear(SystemColors.Control);
if (checkBox1.Checked == true)
{
myPen.Color = Color.Green;
for (x = 0; x < w; x += inc)
gr.DrawLine(myPen, x, pictureBox1.Location.Y, x, h);
for (y = 0; y < h; y += inc)
gr.DrawLine(myPen, pictureBox1.Location.X, y, w, y);
}
Invalidate();
myPen.Dispose();
}
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
grid();
Refresh();
}
private void trackBar1_Scroll(object sender, EventArgs e)
{
grid();
Refresh();
}
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
if (BackImage != null)
e.Graphics.DrawImage(BackImage, 0, 0);
}
答案 0 :(得分:0)
将PictureBox的 Image 属性设置为所需的图像,然后在PictureBox的 Paint()事件中使用提供的private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
pictureBox1.Invalidate();
}
private void trackBar1_Scroll(object sender, EventArgs e)
{
pictureBox1.Invalidate();
}
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
if (checkBox1.Checked)
{
int x, y;
int w = pictureBox1.Size.Width;
int h = pictureBox1.Size.Height;
int inc = trackBar1.Value;
Graphics gr = e.Graphics;
if (checkBox1.Checked == true)
{
for (x = 0; x < w; x += inc)
gr.DrawLine(Pens.Green, x, 0, x, h);
for (y = 0; y < h; y += inc)
gr.DrawLine(Pens.Green, 0, y, w, y);
}
}
}
:>
int main()
{
char file_info[250]="I like pineapples!";
FILE *fptr;
if ((fptr = fopen("try.bin","wb")) == NULL){
printf("Error! opening file");
exit(1);
}
fwrite(&file_info, sizeof(char), 250, fptr);
fclose(fptr);
fptr=fopen("try.bin","rb");
char file_array[250];
fread(file_array,sizeof(char),250,fptr); // read one character
int i=0,num=0,j=0,count=0;
printf("Offset Bytes Characters\n");
printf("------ ----------------------------- ----------");
for(i=0;i<strlen(file_array);i++){
if(i%10==0)
{
if(i>=9){
count=j;
printf(" ");
for(;j<count+10;j++)
{
printf("%c",file_info[j]);
}
}
printf("\n %5d ",num*10);
num++;
}
printf(" %x",file_array[i]);
}
fclose(fptr);
return 0;
}