[求助]一个 pictureBox 画图问题

yun_feiyang 2015-06-10 11:47:49
我的需求是这样的:

1.在 pictureBox 中画18个图片(图片宽高为:20*20),图片以横向顺序排列。
2.前8个图片或后8个图片要按一定角度画折线(图片也要按这个角度旋转)。
3.参数为:起点坐标,终点坐标,角度

希望高人帮助,在此谢谢了!

大致效果是这样的(第一排的样子):
...全文
357 30 打赏 收藏 转发到动态 举报
写回复
用AI写文章
30 条回复
切换为时间正序
请发表友善的回复…
发表回复
夏东铃 2015-06-14
  • 打赏
  • 举报
回复
加方框………
夏东铃 2015-06-14
  • 打赏
  • 举报
回复
怎么有矩形………
enlai123 2015-06-12
  • 打赏
  • 举报
回复
我也想知道这个问题
yun_feiyang 2015-06-11
  • 打赏
  • 举报
回复
再次感谢 xuzuning 版主的帮助,我原来对绘图这块没研究过,一着急就反应慢,问题解决了,谢谢大家!
yun_feiyang 2015-06-11
  • 打赏
  • 举报
回复
引用 22 楼 xuzuning 的回复:


首先谢谢 xuzuning、starfd,和各位回答并关注这个问题的朋友们,问题解决了,谢谢,下面我把代码贴出来,希望对有需要的朋友有点用。


public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.Paint += Form1_Paint;
}

void Form1_Paint(object sender, PaintEventArgs e)
{
Draw(e.Graphics, 80, 100, 340);
Draw(e.Graphics, 80, 200, 30);
Draw(e.Graphics, 80, 300, 30);
}

public void Draw(Graphics g, int x, int y, int angle)
{
var width = 30;
var space = 10;
Pen pen = new Pen(Color.Red, 1);
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.CompositingQuality = CompositingQuality.HighQuality;
g.SmoothingMode = SmoothingMode.HighQuality;

for (var i = 0; i < 15; i++)
{
if (i == 10)
{
var positionX = i * (space + width) + x;
g.TranslateTransform(positionX, y); // 先平移坐标原点
g.RotateTransform(angle); // 再旋转
}
Rectangle rec = new Rectangle();
// 旋转后x,y的坐标要相对新原点的(0,0)来计算,也就是拐弯处的(x,y)
rec.Y = i >= 10 ? 0 : y;
rec.X = (i >= 10 ? i - 10 : i) * (space + width) + (i >= 10 ? space : x);
rec.Width = width;
rec.Height = width;
g.DrawRectangle(pen, rec);
}

g.ResetTransform();
}
}


crazy小新zo 2015-06-11
  • 打赏
  • 举报
回复
有解了没 同求啊有解了没 同求啊
W1020745639 2015-06-11
  • 打赏
  • 举报
回复
value属性值不能删除
yun_feiyang 2015-06-10
  • 打赏
  • 举报
回复
引用 4 楼 FoxDave 的回复:
这用得着PictureBox嘛?用GDI+画画
PictureBox 上画着底图,在底图上画
Justin-Liu 2015-06-10
  • 打赏
  • 举报
回复
这用得着PictureBox嘛?用GDI+画画
yun_feiyang 2015-06-10
  • 打赏
  • 举报
回复
引用 2 楼 starfd 的回复:
你这个是要在图片Image上加方块吧?然后在特定位置还要改变中心点,并且旋转一定角度,继续Draw其它方块? 还有这跟PictureBox有啥关系……
就是在PictureBox上用 graphics.DrawImage来画图片,方块就是图片,现在在特定位置一改变中心点,折的地方坐标就不对了,所以这块困住我了
  • 打赏
  • 举报
回复
你这个是要在图片Image上加方块吧?然后在特定位置还要改变中心点,并且旋转一定角度,继续Draw其它方块? 还有这跟PictureBox有啥关系……
xdashewan 2015-06-10
  • 打赏
  • 举报
回复
gdi+画矩形
xuzuning 2015-06-10
  • 打赏
  • 举报
回复
xuzuning 2015-06-10
  • 打赏
  • 举报
回复
有红块的两段不就是折线形状吗? 你说不叫折线,那说成什么才合适呢?
引用 20 楼 tcmakebest 的回复:
大家都没弄明白,图中没有折线,又如何知道对与不对呢?
tcmakebest 2015-06-10
  • 打赏
  • 举报
回复
引用 14 楼 yun_feiyang 的回复:
[quote=引用 13 楼 Z65443344 的回复:] 所谓有用的代码,是指你自己啥都不用改,就能用的代码吗
看你说的,我代码都写出来了,我帖子中帖出的图就是我用代码画出来的,就是中间拐弯,有折线的地方弄不对,所以才发的帖子,不是说一定要不用改直接用的代码,就是想找到这块的最有效的解决办法,因为是急用,上面的帖子我都看过了,也试过的,因为效果不理想,所以才这样说的![/quote] 大家都没弄明白,图中没有折线,又如何知道对与不对呢?
xuzuning 2015-06-10
  • 打赏
  • 举报
回复
graphics.RotateTransform(angle); 就是问题的根源 因为旋转是以原点(0,0)为中心进行的,而你的那两条线的旋转中心在两线的垂直平分线交点处 你需要将这个中心平移到原点,旋转后再平移回来 这一点在我给你的资料里已经说的很清楚了
存钱买冰棍 2015-06-10
  • 打赏
  • 举报
回复
GDI+最好用
xuzuning 2015-06-10
  • 打赏
  • 举报
回复
你期望的是什么样子 有折线的地方弄不对 是指直线与斜线相接处距离太大?
yun_feiyang 2015-06-10
  • 打赏
  • 举报
回复
引用 15 楼 xuzuning 的回复:
那你贴出你的代码,正好有空帮你改改(50元你就自己买酒喝吧)
好吧,涉及一点行业方面的,我只贴画图这块的

// 画底图
        private void DrawBaseMap(Rectangle rect, Image img, float angle, Rectangle turnRect)
        {
            Graphics graphics = this.pictureBoxBack.CreateGraphics();
            //使绘图质量最高,即消除锯齿
            //graphics.SmoothingMode = SmoothingMode.AntiAlias;
            //graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
            //graphics.CompositingQuality = CompositingQuality.HighQuality;

            graphics.SmoothingMode = SmoothingMode.HighQuality; //高质量
            graphics.PixelOffsetMode = PixelOffsetMode.HighQuality; //高像素偏移质量

            if (angle > 0)
            {
                graphics.TranslateTransform(turnRect.X, turnRect.Y);
                graphics.RotateTransform(angle);
                
                rect = new Rectangle(rect.X - turnRect.X, rect.Y, rect.Width, rect.Height);
            }

            graphics.DrawImage(img, rect);
            graphics.Dispose();
        }

        int UpCount = 0;
        int DownCount = 0;
        Rectangle UpTurnRect = new Rectangle();
        Rectangle DownTurnRect = new Rectangle();

        private void UpdataStorage(Rectangle upZone, Rectangle downZone, List<Storage> storages)
        {
            List<Storage> upstorage = storages.FindAll(delegate(Storage s)
            {
                return s.Position == StoragePosition.UP;
            });
            
            int storeCount = upstorage.Count;
            int count = 1;
            int offset = 10;
            float angle = 0;

            if (0 != storeCount)
            {
                int startX = upZone.Right - offset;
                int startY_UP = upZone.Top;
                //double btnHeight = upZone.Height;
                int btnWidth = (upZone.Width - offset) / storeCount;
                int btnHeight = btnWidth;
                foreach (Storage s in upstorage)
                {
                    Bitmap b = null;
                    string des = string.Empty;
                    switch (s.Status)
                    {
                        case StorageStatus.Empty:
                            b = new Bitmap(Properties.Resources.map_empty);
                            des = "空闲";
                            break;
                        case StorageStatus.Full:
                            b = new Bitmap(Properties.Resources.map_full);
                            des = "占用";
                            break;
                        case StorageStatus.Working:
                            b = new Bitmap(Properties.Resources.map_working);
                            des = "作业中";
                            break;
                    }

                    Rectangle rect = new Rectangle(startX - count * btnWidth,startY_UP,btnWidth - 4,btnHeight);

                    if (s.IsTurn)
                    {
                        if (0 == UpCount)
                            UpTurnRect = rect;
                        angle = 40;
                        UpCount++;
                    }
                    else
                        angle = 0;

                    DrawBaseMap(rect, b, angle, UpTurnRect);
                    count++;
                }
            }

            List<Storage> downstorage = storages.FindAll(delegate(Storage s)
            {
                return s.Position == StoragePosition.Down;
            });
            count = 1;
            storeCount = downstorage.Count;

            if (0 != storeCount)
            {
                int startX = downZone.Right - offset;
                int startY_Down = downZone.Top;
                //int btnHeight = downZone.Height;
                int btnWidth = (downZone.Width - offset) / storeCount;
                int btnHeight = btnWidth;
                foreach (Storage s in downstorage)
                {
                    Bitmap b = null;
                    string des = string.Empty;
                    switch (s.Status)
                    {
                        case StorageStatus.Empty:
                            b = new Bitmap(Properties.Resources.map_empty);
                            des = "空闲";
                            break;
                        case StorageStatus.Full:
                            b = new Bitmap(Properties.Resources.map_full);
                            des = "占用";
                            break;
                        case StorageStatus.Working:
                            b = new Bitmap(Properties.Resources.map_working);
                            des = "作业中";
                            break;
                    }

                    Rectangle rect = new Rectangle(startX - count * btnWidth, startY_Down, btnWidth - 4, btnHeight);

                    if (s.IsTurn)
                    {
                        if (0 == DownCount)
                            DownTurnRect = rect;
                        angle = 40;
                        DownCount++;
                    }
                    else
                        angle = 0;

                    DrawBaseMap(rect, b, angle, DownTurnRect);

                    count++;
                }
            }

        }
xuzuning 2015-06-10
  • 打赏
  • 举报
回复
那你贴出你的代码,正好有空帮你改改(50元你就自己买酒喝吧)
加载更多回复(9)

110,536

社区成员

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

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

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