NGMsoftware

NGMsoftware
로그인 회원가입
  • 매뉴얼
  • 학습
  • 매뉴얼

    학습


    C# 문자열을 직사각형으로 변환하는 방법. (How to convert a String to Rectangle.)

    페이지 정보

    본문

    안녕하세요. 엔지엠소프트웨어입니다. 이 메소드는 "100, 100, 50, 50" 또는 "{100, 100, 50, 50}"과 같은 문자열을 C#의 "System.Drawing.Rectangle" 형식으로 변환하는 메소드입니다. Rectangle은 좌표(Point)와 크기(Size)로 이루어져 있습니다.

    public static Rectangle ConvertRectangle(string rect)
    {
        if (string.IsNullOrEmpty(rect))
            return new Rectangle();
    
        try
        {
            string[] coords = rect.Split(',');
            int x = int.Parse(Regex.Replace(coords[0], @"\D", string.Empty));
            int y = int.Parse(Regex.Replace(coords[1], @"\D", string.Empty));
            int w = int.Parse(Regex.Replace(coords[2], @"\D", string.Empty));
            int h = int.Parse(Regex.Replace(coords[3], @"\D", string.Empty));
    
            return new Rectangle(
                coords[0].Contains("-") ? x * -1 : x, 
                coords[1].Contains("-") ? y * -1 : y, 
                coords[2].Contains("-") ? w * -1 : w, 
                coords[3].Contains("-") ? h * -1 : h);
        }
        catch
        {
            return new Rectangle();
        }
    }

     

    개발자에게 후원하기

    MGtdv7r.png

     

    추천, 구독, 홍보 꼭~ 부탁드립니다.

    여러분의 후원이 빠른 귀농을 가능하게 해줍니다~ 답답한 도시를 벗어나 귀농하고 싶은 개발자~

    감사합니다~

    • 네이버 공유하기
    • 페이스북 공유하기
    • 트위터 공유하기
    • 카카오스토리 공유하기
    추천0 비추천0

    댓글목록

    등록된 댓글이 없습니다.