2009-06-03 2 views
0

저는 일부 usercontrols를 사용하는 앱을 가지고 있습니다. 내가 사용하기 위해로드 될 때 usercontrols의 축소판 이미지를 가져 와서 flowlayout 패널에 추가하고 싶습니다..NET Winform - 사용자 정의 컨트롤의 축소판

로드 된 사용자 정의 컨트롤의 축소판 이미지를 만드는 방법에 대한 정보는 어디에서 찾을 수 있습니까?

내가이 표시되기 전에 그것을 할 수있는 방법을 알고하지 않습니다하지만 화면에 한 번이 같은 방법 사용할 수

답변

2

:

private Image GetControlThumb(Control control, int thumbSize) 
{ 
    Bitmap imgLarge = new Bitmap(control.Bounds.Width, control.Bounds.Height); 
    using (Graphics g = Graphics.FromImage(imgLarge)) 
    { 
     g.CopyFromScreen(
      control.Parent.PointToScreen(new Point(control.Left, control.Top)), 
      new Point(0, 0), 
      new Size(control.Bounds.Width, control.Bounds.Height)); 
    } 


    Size size; 
    if (control.Width > control.Height) 
    { 
     size = new Size(thumbSize, (int)(thumbSize * (float)control.Height/(float)control.Width)); 
    } 
    else 
    { 
     size = new Size((int)(thumbSize * (float)control.Width/(float)control.Height), thumbSize); 
    } 
    Image imgSmall = imgLarge.GetThumbnailImage(size.Width, size.Height, new Image.GetThumbnailImageAbort(delegate { return false; }), IntPtr.Zero); 
    imgLarge.Dispose(); 
    return imgSmall; 

} 

당신이 얻을하는 데 사용할 수 있습니다 다음과 같은 컨트롤의 축소판 그림 :

+0

버튼을 클릭하면 디스크에 이미지를 저장할 수 있지만 사용자 컨트롤 laod에서는 흰색 이미지가 나타나며 그림판()을 사용할 수 있습니까? 이것은 사용자 정의 컨트롤에서 여러 번 호출됩니다. –

+0

Using 블록의 목적은 무엇입니까? –

+0

Graphics 클래스는 IDisposable을 구현합니다. using 블록은 Dispose 메서드가 호출되어 Graphics 객체가 리소스를 해제 할 수 있도록합니다. –