Set quality of bmp object without saving to file

  • Thread starter Thread starter theblacksnake
  • Start date Start date
T

theblacksnake

Guest
Hi all,

So I kinda making a remote desktop software. I am using the following code to get the screen into a bitmap object and then serialize it into the network stream. I would like to set the quality of the image. In this article it explains how to do so EncoderParameter Constructor (Encoder, Int64) (System.Drawing.Imaging). However, it assumes that you want to save the bmp object to a file while in my case I just want to serialize it to the stream. One solution would be to save the image with the preferred quality and then open it again into a new bmp object and serialize it to the stream. However this seems unnecessary. Does anybody know how to change the quality of the image without having to save it to a file ?

----------------------------------
Dim bf As New BinaryFormatter
nstream = client.GetStream

Dim g As Graphics = Graphics.FromHwnd(IntPtr.Zero)
Dim desktop As IntPtr = g.GetHdc()
Dim currentScreen As Rectangle
currentScreen.Height = GetDeviceCaps(desktop, 117).ToString
currentScreen.Width = GetDeviceCaps(desktop, 118).ToString
Dim bmp As New Bitmap(currentScreen.Width, currentScreen.Height)
Dim gg = Graphics.FromImage(bmp)
gg.CopyFromScreen(New Point(0, 0), New Point(0, 0), currentScreen.Size)

myImageCodecInfo = GetEncoderInfo(ImageFormat.Jpeg)
myEncoder = Encoder.Quality
myEncoderParameters = New EncoderParameters(1)
myEncoderParameter = New EncoderParameter(myEncoder, CType(RDesktopQuality, Int32))
myEncoderParameters.Param(0) = myEncoderParameter

Dim name As String = My.Computer.FileSystem.SpecialDirectories.Temp & "\" & FilenameByDate()
bmp.Save(Name, myImageCodecInfo, myEncoderParameters)

Dim scr As New Bitmap(Name)
bf.Serialize(nstream, scr)
scr.Dispose()
g.Dispose()
bmp.Dispose()
gg.Dispose()
My.Computer.FileSystem.DeleteFile(name)
-------------------------------------
Private Shared Function GetEncoderInfo(ByVal format As ImageFormat) As ImageCodecInfo
Dim j As Integer
Dim encoders() As ImageCodecInfo
encoders = ImageCodecInfo.GetImageEncoders()

j = 0
While j < encoders.Length
If encoders(j).FormatID = format.Guid Then
Return encoders(j)
End If
j += 1
End While
Return Nothing

End Function 'GetEncoderInfo

Continue reading...
 
Back
Top