我懂得用winsock控件向远程计算机发送字符。那么,如果我想:把包含图象信息的数据用winsock来发送给远程计算机,应该怎么做呢?

解决方案 »

  1.   

    关于BMP 转换 JPEG 我是这样做的(刚刚完成),将picture1传给DELPHI,压缩在JPEG流,
    DELPHI 源码library Project2;{ Important note about DLL memory management: ShareMem must be the
      first unit in your library's USES clause AND your project's (select
      Project-View Source) USES clause if your DLL exports any procedures or
      functions that pass strings as parameters or function results. This
      applies to all strings passed to and from your DLL--even those that
      are nested in records and classes. ShareMem is the interface unit to
      the BORLNDMM.DLL shared memory manager, which must be deployed along
      with your DLL. To avoid using BORLNDMM.DLL, pass string information
      using PChar or ShortString parameters. }uses
      Windows,
      SysUtils,
      Classes,
      Graphics,
      Forms,
      JPEG;
    function BmpToJpegStream(Picdc: hdc; picWidth:integer; picHeigt:integer; data:pbyte; count:integer; Q:integer):integer;stdcall;
        var
          MyBmp: TBitmap;
          Myjpg: TJpegimage;
          Mystream: TMemorystream;
          Mycan: Tcanvas;
          R: TRect;    begin      mybmp := TBitmap.Create;
          myjpg := TJpegimage.Create;
          Mystream := TMemorystream.Create;      Mycan := TCanvas.Create;      try
            Mycan.Handle := picdc;
            R := Rect(0, 0, picWidth , picHeigt);
            Mybmp.Width := R.Right;
            Mybmp.Height := R.Bottom;
            Mybmp.Canvas.CopyRect(R, Mycan, R);
          finally
            releaseDC(0, picDC);
          end;
          Mycan.Handle := 0;
          Mycan.Free;      myjpg.Assign(mybmp);
          myjpg.CompressionQuality :=q;
          myjpg.SaveToStream(mystream);      bmptojpegstream := mystream.size;
          if mystream.size < count then
          begin
            copymemory(data,mystream.Memory  ,mystream.Size  );
          end;      mybmp.Free;
          myjpg.Free;
          mystream.Free;
        end;
    function GetDeskJpegStream(data:pbyte; count:integer; Q:integer; DrawCur: boolean):integer;stdcall;
        var
          MyBmp: TBitmap;
          Myjpg: TJpegimage;
          Mystream: TMemorystream;      dc: hdc;
          Cursorx, Cursory: integer;
          Mycan: Tcanvas;
          R: TRect;
          DrawPos: TPoint;
          MyCursor: TIcon;
          hld: hwnd;
          Threadld: dword;
          mp: tpoint;
          pIconInfo: TIconInfo;    begin      mybmp := TBitmap.Create;
          myjpg := TJpegimage.Create;
          Mystream := TMemorystream.Create;      Mycan := TCanvas.Create;
          dc := getwindowdc(0);
          try
            Mycan.Handle := dc;
            R := Rect(0, 0, screen.Width , screen.Height );
            Mybmp.Width := R.Right;
            Mybmp.Height := R.Bottom;
            Mybmp.Canvas.CopyRect(R, Mycan, R);
          finally
            releaseDC(0, DC);
          end;
          Mycan.Handle := 0;
          Mycan.Free;      if DrawCur then
          begin
            GetCursorPos(DrawPos);
            MyCursor := TIcon.Create;
            getcursorpos(mp);
            hld := WindowFromPoint(mp);
            Threadld := GetWindowThreadProcessId(hld, nil);
            AttachThreadInput(GetCurrentThreadId, Threadld, True);
            MyCursor.Handle := Getcursor();
            AttachThreadInput(GetCurrentThreadId, threadld, False);
            GetIconInfo(Mycursor.Handle, pIconInfo);
            cursorx := DrawPos.x - round(pIconInfo.xHotspot);
            cursory := DrawPos.y - round(pIconInfo.yHotspot);
            Mybmp.Canvas.Draw(cursorx, cursory, MyCursor);
            DeleteObject(pIconInfo.hbmColor);{GetIconInfo 使用时创建了两个bitmap对象. 需要手工释放这两个对象}
            DeleteObject(pIconInfo.hbmMask);
            Mycursor.ReleaseHandle;
            MyCursor.Free;
          end;      myjpg.Assign(mybmp);
          myjpg.CompressionQuality :=q;
          myjpg.SaveToStream(mystream);      GetDeskJpegStream := mystream.size;
          if mystream.size < count then
          begin
            copymemory(data,mystream.Memory  ,mystream.Size  );
          end;      mybmp.Free;
          myjpg.Free;
          mystream.Free;
        end;function LoadBmpFromJpegStream(Data:pbyte; Count:integer; AddressofProce:pinteger ):integer;stdcall;
        var
          MyBmp: TBitmap;
          Myjpg: TJpegimage;
          Mystream: TMemorystream;
          pichdc: integer;
          CallBackProce: function(Width: integer; Height: integer): Integer; stdcall;    begin
          @CallBackProce := AddressofProce;
          mybmp := TBitmap.Create;
          myjpg := TJpegimage.Create;
          Mystream := TMemorystream.Create;      mystream.SetSize(count);
          mystream.Position := 0;
          copymemory(mystream.Memory,data,count);
          myjpg.LoadFromStream(mystream);
          mybmp.Assign(myjpg);      pichdc := callbackproce(myjpg.Width ,myjpg.Height);      bitblt(picHdc,0,0,mybmp.Width ,mybmp.Height ,mybmp.Canvas.Handle,0,0,SRCCOPY );      LoadBmpFromJpegStream :=pichdc;      mybmp.Free;
          myjpg.Free;
          mystream.Free;
        end;  exports
        BmpToJpegStream name 'BmpToJpegStream',
        GetDeskJpegStream name 'GetDeskJpegStream',
        LoadBmpFromJpegStream name 'LoadBmpFromJpegStream';  {$R *.RES}
    begin
    end.
    下贴为VB代码
      

  2.   

    将DELPHI生成DLL文件后 在VB中调用FORM中有三个按钮:command2,command3,command4,
    一个picture:picture1 
    注意: picture1.scalemode=3,form1.scalemode=3,picture1.autoredraw=true'form代码
    Private Declare Function BmpToJpegStream Lib "C:\Documents and Settings\Administrator\桌面\New\BmpToJpeg\Project2.dll" _
       (ByVal hdc As Long, ByVal width As Long, ByVal height As Long, _
       DataBuf As Any, ByVal Count As Long, _
       ByVal Quailty As Long) As Long  Private Declare Function GetDeskJpegStream Lib "C:\Documents and Settings\Administrator\桌面\New\BmpToJpeg\Project2.dll" _
       (DataBuf As Any, ByVal Count As Long, _
       ByVal Quailty As Long, ByVal DrawCursor As Boolean) As Long
       
    Private Declare Function LoadBmpFromJpegStream Lib "C:\Documents and Settings\Administrator\桌面\New\BmpToJpeg\Project2.dll" _
       (DataBuf As Any, ByVal Count As Long, _
        ByVal CallBackProc As Long) As Long
       Private Sub Command2_Click()   '将picture1中的图片,转换为JPEG流
      Dim m As Double
      Dim n As Long
      Dim data() As Byte
      
      m = 1024# * 100 * 3         '要定义足够大
      ReDim data(m) As Byte
      
      n = BmpToJpegStream(Picture1.hdc, Picture1.width, Picture1.height, data(0), m, ByVal 10)   'byval 10 此处为JPEG文件压缩率值为1到100.
      
    '执行完成后得到数据流在DATA中,N为JPEG数据流大小,这样就可以直接发送了,不用使用文件中转
      If n > 0 And n < m Then
        ReDim Preserve data(n - 1) As Byte          
        Open "c:\ddddd.jpg" For Binary As #1
          Put #1, , data
        Close #1
      End If
      
    End Sub
    Private Sub Command3_Click()     '直接使用DELPHI捕捉整个屏幕,并压缩为JPEG数据流
      Dim m As Double
      Dim n As Long
      Dim data() As Byte
      
      m = 1024# * 100 * 3
      ReDim data(m) As Byte
      n = GetDeskJpegStream(data(0), m, 10, True)  'true为捕捉鼠标 ,10为JPEG压缩率  '执行完成后得到数据流在DATA中,N为JPEG数据流大小  
      If n > 0 And n < m Then
        ReDim Preserve data(n - 1) As Byte
        Open "c:\ddddd.jpg" For Binary As #1
          Put #1, , data
        Close #1
      End If
    End Sub
    Private Sub Command4_Click()    '将JPEG数据流显示为图片
      Dim data() As Byte
      Dim m As Long
      m = FileLen("c:\ddddd.jpg")
      
      Open "c:\ddddd.jpg" For Binary As #1
      m = LOF(1)
      ReDim data(m - 1) As Byte
      
      Get #1, , data
      Close #1
      Dim w As Long, h As Long
      Debug.Print Picture1.width, Picture1.height
      Debug.Print LoadBmpFromJpegStream(data(0), m, AddressOf CallBackSetPicWidth)
      '此处使用了回调函数,来更改picture1.的大小,否则会发生picture1显示图片不全的情况
      '当然在编的时候我也考虑过先获得JPEG图片的大小,然后第二次调用显示图片,考虑再三,使用上一情况,自认为可以提高效率.
     
      Picture1.Refresh
    End Sub'下面还有一个BAS 代码
    ''BAS
    Public Function CallBackSetPicWidth(ByVal width As Long, ByVal height As Long) As Long
      Form1.Picture1.width = width                '设置大小
      Form1.Picture1.height = height
      CallBackSetPicWidth = Form1.Picture1.hdc    '返回picture1.HDC,
    End Function''程序到此结束,这个程序是我学习DELPHI五天后编写,就是为了生成JPEG流而学,难免会有编写规范及方法上的错误,如有好的建议的方法,请多多指教.
    你可以将一个图片生成流后,发出
    接到后,显示图片,
      

  3.   

    "你可以将一个图片生成流后,发出
    接到后,显示图片,"楼上,我就是想问怎么发送出去生成的流?关于"BMP 转换 JPEG",我可以用在VC里用MFC来做个DLL
      

  4.   

    如果是Winsock控件,图像文件是"c:\a.jpg"dim temp() as byte
    dim m as long
    open "c:\a.jpg" for binary as #1
    m=lof(1)
    redim temp(m-1) as byte
    get #1,,temp
    close #1
    winscok1.SendData temp     'temp就是包括流的字节数组你还可以用Winsock1_SendComplete()事件来获得数据已经发送完毕的消息,对端接收数据完成后
    open "c:\b.jpg" for binary as #1
    put #1,,temp
    close #1
      

  5.   

    传大文件 我一般把文件转换成BASE64 编码  用来发文本格式 那出问题的概率就小多了, 当然这样数据量大了一些  但代码好写多了