2012-09-16 5 views
1

이미지를 특정 크기로 크기를 조정하려하지만 선택한 크기보다 작 으면 이미지를 전혀 늘리지 않으려 고합니다. 대신 사용하지 않는 이미지 영역 주위에 검정색 배경을 추가하려고합니다.VB.NET - 이미지 만들기 - 크기 조정하지만 남은 공간에 배경 추가

나는이 작업을 수행하는 가장 쉬운 방법은 다음이 배경의 맨 위에 & 센터에게 이미지를 추가 배경색 &을 설정 & 내 원하는 크기의 새로운 이미지를 만들 수있을 것이라고 생각합니다. 내가 사용 비트 맵을 만든

: 나는 과정을 완료하는 방법에 대한 손실 조금있어이 시점에서

Dim bmp As New Drawing.Bitmap(500, 500) 
Dim grap As Drawing.Graphics = Drawing.Graphics.FromImage(bmp) 
grap.Clear(Drawing.Color.Black) 

이 필요한 모든 비트 맵 & 센터에 이미지를 추가하는 것입니다.

grap.Clear(Drawing.Color.Black) 

반드시 단지 전체 그림을 다시 닦아 것 :

어떤 아이디어가 많이 나는이 테스트를하지 않은하지만 당신은 거의 당신이 원하는 것을 가지고처럼 보이지만

답변

2

을 appretiated 것 검은 색으로.

그리기 이미지에 분명 이전을 수행하십시오 :

Graphics pic = this.CreateGraphics(); 
pic.Clear(Color.Black); 
pic.DrawImage(img, new Point(center)); 
+0

감사합니다. 센터 옵션을 사용할 수 없다는 것을 알지 못했습니다. 아래 답변을 게시했습니다. 가로 세로 비율, 크기 조정 등) – Chris

+0

센터는 내가 만든 변수 일뿐입니다. –

0

사용하여 종료 :

' Load Image 
Dim FilePath As String = "testimage.jpg" 
Dim OriginalImage As New Bitmap(FilePath) 

' Resize Image While Maintaining Aspect Ratio 
Dim aspectRatio As Double 
Dim newHeight As Integer 
Dim newWidth As Integer 
Dim maxWidth As Integer = 500 
Dim maxHeight As Integer = 500 

' Calculate Size 
If OriginalImage.Width > maxWidth Or OriginalImage.Height > maxHeight Then 
    If OriginalImage.Width >= OriginalImage.Height Then ' image is wider than tall 
     newWidth = maxWidth 
     aspectRatio = OriginalImage.Width/maxWidth 
     newHeight = CInt(OriginalImage.Height/aspectRatio) 
    Else ' image is taller than wide 
     newHeight = maxHeight 
     aspectRatio = OriginalImage.Height/maxHeight 
     newWidth = CInt(OriginalImage.Width/aspectRatio) 
    End If 
Else ' if image is not larger than max then increase size 
    If OriginalImage.Width > OriginalImage.Height Then 
     newWidth = maxWidth 
     aspectRatio = OriginalImage.Width/maxWidth 
     newHeight = CInt(OriginalImage.Height/aspectRatio) 
    Else 
     newHeight = maxHeight 
     aspectRatio = OriginalImage.Height/maxHeight 
     newWidth = CInt(OriginalImage.Width/aspectRatio) 
    End If 

    ' Below keeps original height & width instead of resizing to fit new height/width 
    ' newWidth = OriginalImage.Width 
    ' newHeight = OriginalImage.Height 
End If 

Dim newImg As New Bitmap(OriginalImage, CInt(newWidth), CInt(newHeight)) '' blank canvas 
' Create New Bitmap 
Dim bmp As New Drawing.Bitmap(500, 500) 
Dim grap As Drawing.Graphics = Drawing.Graphics.FromImage(bmp) 
grap.Clear(Drawing.Color.Black) 
Dim g As Graphics = Graphics.FromImage(bmp) 

' Calculate Points To Insert Resized Image 
Dim InsertX As Integer 
Dim InsertY As Integer 

' Calculate Y Axis Point 
If newImg.Height >= 500 Then 
    InsertY = 0 
Else 
    InsertY = CInt(((500 - newImg.Height)/2)) 
End If 

' Calculate X Axis Point 
If newImg.Width >= 500 Then 
    InsertX = 0 
Else 
    InsertX = CInt(((500 - newImg.Width)/2)) 
End If 

' Add Resized Image To Canvas 
g.DrawImage(newImg, New Point(InsertX, InsertY)) 
0

큰 축 단지 비율을 사용하면 다른 그 상황에서 실패 할 수 있습니다 비율은 다른 축을 초과하도록합니다. , 즉 1400x1000 -> (300x200에 적합) ->하지만 1400/300 비율 (4.6)이면 300x214가됩니다. 두 비율을 확인하고 더 큰 것으로 계속하는 것이 유용 할 것이라고 생각합니다.