PDA

View Full Version : سوال: Stretch كردن يك تصوير با كد



Neeloofar
دوشنبه 27 اردیبهشت 1389, 14:02 عصر
سلام
من با كد زير عكس پرسنلي رو از ديتابيس مي گيرم و در خصيصه Image كنترلي به نام CTLImage قرار ميدم. كنترل CTLImage خاصيت Stretch رو نداره و ممكنه عكسهاي پرسنلي در ديتابيس با ابعاد مختلف ذخيره شده باشند.
چطور ميشه تصويري كه از Image.FromStream ميگيرم با كدنويسي به اندازه دلخواه دربيارم و سپس به CTLImage ست كنم؟


If Not IsDBNull(dt.Rows(0).Item("Picture")) Then
Dim Bt() As Byte = CType(dt.Rows(0).Item("Picture"), Byte())
Dim MS As New System.IO.MemoryStream(Bt)
CTLImage‍.Image = Image.FromStream(MS)
MS.Dispose()
End Ifبا تشكر.

alireza_s_84
دوشنبه 27 اردیبهشت 1389, 14:57 عصر
سلام دوست عزیز:

Dim Bt() As Byte = CType(dt.Rows(0).Item("Picture"), Byte())
CTLImage.Image = ResizeImage(Bt, New Size(300, 400))

Private Function ResizeImage(ByVal Bt() As Byte, ByVal newSize As Size) As Image
Try
Dim strm As New System.IO.MemoryStream(Bt)
Dim bmp As New Bitmap(newSize.Width, newSize.Height)
Dim img As Image = Image.FromStream(strm)
Dim g As Graphics = Graphics.FromImage(bmp)
Dim rec As New Rectangle(0, 0, newSize.Width, newSize.Height)
g.DrawImage(img, rec, 0, 0, img.Width, img.Height, GraphicsUnit.Pixel)
g.Dispose()
strm.Dispose()
GC.Collect()
Return CType(bmp, Image)
Catch ex As Exception
Return Nothing
End Try
End Function

موفق باشی