画图控制位置 drawimage

sdfgrtyu 2017-06-14 09:08:06
private void panel1_Paint_1(object sender, PaintEventArgs e)
{
Graphics g1 = e.Graphics;
if (image != null)
{
if (c == 0)
{
g1.DrawImage(image, new RectangleF(groupBox2.Width, 500, image.Width, Convert.ToSingle((image.Height * sb1 * 1.0))));
}
else if (c == 1)
{
g1.DrawImage(image, new RectangleF(groupBox2.Width, 200, this.panel1.Width, Convert.ToSingle((image.Height * sb1 * 1.0))));
}
}

if (image2 != null)
{
if (c == 0)
{
g1.DrawImage(image2, new RectangleF(groupBox2.Width, 400, image2.Width, Convert.ToSingle((image2.Height * sb1 * 1.0))));
}
else if (c == 1)
{
g1.DrawImage(image2, new RectangleF(0, this.panel1.Height / 3, this.panel1.Width, Convert.ToSingle((image2.Height * sb1 * 1.0))));
}
}

if (image3 != null)
{
if (c == 0)
{
g1.DrawImage(image3, new RectangleF(groupBox2.Width, 700, image3.Width, Convert.ToSingle((image3.Height * sb1 * 1.0))));
}
else if (c == 1)
{
g1.DrawImage(image3, new RectangleF(0, this.panel1.Height / 3 * 2, this.panel1.Width, Convert.ToSingle((image3.Height * sb1 * 1.0))));
}
}
}


代码如上面,但是位置不对,只有第一个设置的位置生效,第二个和第三个都和第一个挤在一起了,如何解决,什么原因?
...全文
262 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
sdfgrtyu 2017-06-14
  • 打赏
  • 举报
回复
引用 15 楼 xdashewan 的回复:
[quote=引用 14 楼 u010941149 的回复:] 我只画两个图形,他却出来三个图形,
所以我觉得压根就不是这段代码问题,是不是你image内容就不对,1个image画了不止一个图像[/quote] 找到问题了啊,眼睛有点不好使啊
sdfgrtyu 2017-06-14
  • 打赏
  • 举报
回复
引用 15 楼 xdashewan 的回复:
[quote=引用 14 楼 u010941149 的回复:] 我只画两个图形,他却出来三个图形,
所以我觉得压根就不是这段代码问题,是不是你image内容就不对,1个image画了不止一个图像[/quote] 没有啊
   if (c == 0)
                          {
                              image = new Bitmap(this.panel1.Width / 30 * (j / 5 + 1), 200);
                              image2 = new Bitmap(this.panel1.Width / 30 * (j / 5 + 1), 200);
                              image3 = new Bitmap(this.panel1.Width / 30 * (j / 5 + 1), 200);

                          }
                          else if (c == 1)
                          {
                              image = new Bitmap(this.panel1.Width, 200);
                              image2 = new Bitmap(this.panel1.Width, 200);
                              image3 = new Bitmap(this.panel1.Width, 200);
                          }
                          Graphics g = Graphics.FromImage(image);
                          g.SmoothingMode = SmoothingMode.AntiAlias;
                          //g1.Clear(this.panel1.BackColor);
                          g.InterpolationMode = InterpolationMode.HighQualityBicubic;
                          g.TranslateTransform(1, 10);
                          g.CompositingQuality = CompositingQuality.HighQuality;
                          Pen p = new Pen(Color.Red, 1);//定义了一个红色,宽度为1的画笔    

                          g.DrawLines(p, list2.ToArray());
                          p.Dispose();
                          g.Dispose();
                  
                          Graphics gg = Graphics.FromImage(image);
                          gg.TranslateTransform(1, 10);

                          Pen pp = new Pen(Color.Green, 1);//定义了一个红色,宽度为1的画笔   
                          gg.SmoothingMode = SmoothingMode.AntiAlias;
                          //g1.Clear(this.panel1.BackColor);
                          gg.InterpolationMode = InterpolationMode.HighQualityBicubic;
                          gg.CompositingQuality = CompositingQuality.HighQuality;


                          gg.TranslateTransform(1, 10);

                          gg.DrawLines(pp, list22.ToArray());
                          pp.Dispose();

                          gg.Dispose();
                          Graphics ggg = Graphics.FromImage(image);
                          Pen ppp = new Pen(Color.Blue, 1);//定义了一个红色,宽度为1的画笔   
                          ggg.SmoothingMode = SmoothingMode.AntiAlias;
                          //g1.Clear(this.panel1.BackColor);
                          ggg.InterpolationMode = InterpolationMode.HighQualityBicubic;
                          ggg.CompositingQuality = CompositingQuality.HighQuality;
                          // p = new Pen(Color.Red, 1);//定义了一个红色,宽度为1的画笔    

                          ggg.TranslateTransform(1, 10);
                        
                         
                          ggg.DrawLines(ppp, list222.ToArray());
                     
                      
                          ppp.Dispose();
                      
                         
                          ggg.Dispose();
                          this.panel1.Invalidate();
xdashewan 2017-06-14
  • 打赏
  • 举报
回复
引用 14 楼 u010941149 的回复:
我只画两个图形,他却出来三个图形,
所以我觉得压根就不是这段代码问题,是不是你image内容就不对,1个image画了不止一个图像
sdfgrtyu 2017-06-14
  • 打赏
  • 举报
回复
引用 13 楼 xdashewan 的回复:
只留g1.DrawImage(image, new RectangleF(100, 100, 200, 300));
g1.DrawImage(image2, new RectangleF(100, 500, 200, 300));
这两句代码试试

Graphics g1 = e.Graphics;
// var cn = g1.BeginContainer();
if (image != null)
{

g1.DrawImage(image, new RectangleF(100, 100, 200, 300));
g1.DrawImage(image2, new RectangleF(100, 500, 200, 300));
//if (c == 0)
//{
// g1.DrawImage(image, new RectangleF(groupBox2.Width, 0, image.Width, Convert.ToSingle((image.Height * sb1 * 1.0))));
//}
//else if (c == 1)
//{
// g1.DrawImage(image, new RectangleF(groupBox2.Width, 200, this.panel1.Width, Convert.ToSingle((image.Height * sb1 * 1.0))));
//}
}


我只画两个图形,他却出来三个图形,
xdashewan 2017-06-14
  • 打赏
  • 举报
回复
只留g1.DrawImage(image, new RectangleF(100, 100, 200, 300)); g1.DrawImage(image2, new RectangleF(100, 500, 200, 300)); 这两句代码试试
sdfgrtyu 2017-06-14
  • 打赏
  • 举报
回复
引用 8 楼 xdashewan 的回复:
[quote=引用 7 楼 u010941149 的回复:]
我试验了,写成死值没有变化啊,第一个和第二个还是挤在一起啊

那就是你代码压根没走到,或者被别的代码改变位置[/quote]


就是这样
sdfgrtyu 2017-06-14
  • 打赏
  • 举报
回复
引用 9 楼 Imaor 的回复:
设断点,调……
掉了,没发现问题
sdfgrtyu 2017-06-14
  • 打赏
  • 举报
回复
引用 8 楼 xdashewan 的回复:
[quote=引用 7 楼 u010941149 的回复:] 我试验了,写成死值没有变化啊,第一个和第二个还是挤在一起啊
那就是你代码压根没走到,或者被别的代码改变位置[/quote] 走到了,别的地方也没有改变他位置的代码啊
IEEE_China 2017-06-14
  • 打赏
  • 举报
回复
设断点,调……
xdashewan 2017-06-14
  • 打赏
  • 举报
回复
引用 7 楼 u010941149 的回复:
我试验了,写成死值没有变化啊,第一个和第二个还是挤在一起啊
那就是你代码压根没走到,或者被别的代码改变位置
sdfgrtyu 2017-06-14
  • 打赏
  • 举报
回复
引用 6 楼 xdashewan 的回复:
[quote=引用 5 楼 u010941149 的回复:] 都写死了,还是挤在一起啊
c是0还是1,另外image2的y坐标是500,高度300,image3的y坐标才700,肯定有100高度部分重叠[/quote] 我试验了,写成死值没有变化啊,第一个和第二个还是挤在一起啊
xdashewan 2017-06-14
  • 打赏
  • 举报
回复
引用 5 楼 u010941149 的回复:
都写死了,还是挤在一起啊
c是0还是1,另外image2的y坐标是500,高度300,image3的y坐标才700,肯定有100高度部分重叠
sdfgrtyu 2017-06-14
  • 打赏
  • 举报
回复
引用 3 楼 xdashewan 的回复:
[quote=引用 2 楼 u010941149 的回复:] y值对啊,设置的500,400,还有700啊,但是都挤在一起了
全部写成死值试试,我认为还是参数的问题[/quote] 都写死了,还是挤在一起啊
sdfgrtyu 2017-06-14
  • 打赏
  • 举报
回复
引用 3 楼 xdashewan 的回复:
[quote=引用 2 楼 u010941149 的回复:] y值对啊,设置的500,400,还有700啊,但是都挤在一起了
全部写成死值试试,我认为还是参数的问题[/quote]
     private void panel1_Paint_1(object sender, PaintEventArgs e)
        {
            Graphics g1 = e.Graphics;
            if (image != null)
            {
                if (c == 0)
                {
                    g1.DrawImage(image, new RectangleF(100, 100, 200, 300));
                }
                else if (c == 1)
                {
                    g1.DrawImage(image, new RectangleF(groupBox2.Width, 200, this.panel1.Width, Convert.ToSingle((image.Height * sb1 * 1.0))));
                }
            }
            if (image2 != null)
            {
                if (c == 0)
                {
                    g1.DrawImage(image2, new RectangleF(100, 500, 200, 300));
                }
                else if (c == 1)
                {
                    g1.DrawImage(image2, new RectangleF(0, this.panel1.Height / 3, this.panel1.Width, Convert.ToSingle((image2.Height * sb1 * 1.0))));
                }
            }
            if (image3 != null)
            {
                if (c == 0)
                {
                    g1.DrawImage(image3, new RectangleF(100, 700, 200, 300));
                }
                else if (c == 1)
                {
                    g1.DrawImage(image3, new RectangleF(0, this.panel1.Height / 3 * 2, this.panel1.Width, Convert.ToSingle((image3.Height * sb1 * 1.0))));
                }
            }
        }
xdashewan 2017-06-14
  • 打赏
  • 举报
回复
引用 2 楼 u010941149 的回复:
y值对啊,设置的500,400,还有700啊,但是都挤在一起了
全部写成死值试试,我认为还是参数的问题
sdfgrtyu 2017-06-14
  • 打赏
  • 举报
回复
引用 1 楼 bloodish 的回复:

RectangleF(float x, float y, float width, float height);
y值不对
y值对啊,设置的500,400,还有700啊,但是都挤在一起了
bloodish 2017-06-14
  • 打赏
  • 举报
回复

RectangleF(float x, float y, float width, float height);
y值不对

110,538

社区成员

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

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

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