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();
}
}
개발자에게 후원하기
추천, 구독, 홍보 꼭~ 부탁드립니다.
여러분의 후원이 빠른 귀농을 가능하게 해줍니다~ 답답한 도시를 벗어나 귀농하고 싶은 개발자~
감사합니다~
추천0 비추천0
- 이전글매크로에서 마우스가 자연스럽게 이동할 수 있도록 변환하는 메소드. 21.05.08
- 다음글문자열을 사이즈로 변환하는 방법. (How to convert a String to Size.) 21.05.08
댓글목록
등록된 댓글이 없습니다.