Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/delphi/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Image Delphi:传送图像_Image_Delphi_Stream_Transfer - Fatal编程技术网

Image Delphi:传送图像

Image Delphi:传送图像,image,delphi,stream,transfer,Image,Delphi,Stream,Transfer,德尔福2010 我正在通过使用UTF-8的自定义TCP套接字控件传输图像 客户端 var TSS: TStringStream; STR :String; JPG:TJPEGImage; BMP:TBitmap; begin Try BMP.LoadFromFile('C:\1.bmp'); JPG.Assign(BMP); JPG.CompressionQuality:=80; JPG.Compress; TSS:=TStringStream.Create; JPG.SaveToStrea

德尔福2010

我正在通过使用UTF-8的自定义TCP套接字控件传输图像

客户端

var
TSS: TStringStream;
STR :String;
JPG:TJPEGImage;
BMP:TBitmap;
begin
Try
BMP.LoadFromFile('C:\1.bmp');

JPG.Assign(BMP);
JPG.CompressionQuality:=80;
JPG.Compress;

TSS:=TStringStream.Create;
JPG.SaveToStream(TSS);

STR:=TSS.DataString;

MyTCPSocket.SendString(STR);


finally
BMP.free;
JPG.free;
TSS.free;
end;
end;
服务器端

Var
TSS: TStringStream;

TSS:=TStringStream.Create;
TSS.WriteString(STR);
TSS.SaveToFile('C:\2.jpg');
这段代码在同一台pc上运行非常好

将图像发送到其他使用不同编码的pc时出现的问题 它接收到图像,但我看到数据中有许多错误字符“??”

我认为当TStringStream将字节写入文件时,它无法将unicode字符转换为字节,因此看起来像“?”


非常感谢您的任何想法

您正在尝试发送二进制数据,就像它是UTF-8编码的文本一样它不是,所以不要尝试这样做!以原始二进制形式发送二进制数据,例如:

var
  MS: TMemoryStream;
  JPG: TJPEGImage;
  BMP: TBitmap;
begin
  MS := TMemoryStream.Create;
  try
    JPG := TJPEGImage.Create;
    try
      BMP := TBitmap.Create;
      try
        BMP.LoadFromFile('C:\1.bmp');
        JPG.Assign(BMP);
      finally
        BMP.Free;
      end;
      JPG.CompressionQuality := 80;
      JPG.Compress;
      JPG.SaveToStream(MS);
    finally
      JPG.Free;
    end;
    MS.Position := 0;
    MyTCPSocket.SendStream(MS);
  finally
    MS.free;
  end;
end;

如果必须以文本形式发送二进制数据,则需要使用真正的二进制到文本编码算法对数据进行编码,例如base64或yEnc、notUTF-8(该算法仅用于编码Unicode文本,而非二进制数据),例如:


Thx Remy我的问题是,使用的套接字仅支持发送utf8字符串。我使用EncodeStream函数从“EncdDecd”将流编码到base64 Thx,再次获得您的帮助:)
var
  MS: TMemoryStream;
begin
  MS := TMemoryStream.Create;
  try
    MyTCPSocket.ReadStream(MS);
    MS.Position := 0;
    MS.SaveToFile('C:\2.jpg');
  finally
    MS.Free;
  end;
end;
// TIdEncoderMIME and TIdDecoderMIME are part of Indy,
// which ships with Delphi, but you can use whatever
// you want...
uses
  ..., IdCoderMIME;

var
  S: String;
  MS: TMemoryStream;
  JPG: TJPEGImage;
  BMP: TBitmap;
begin
  MS := TMemoryStream.Create;
  try
    JPG := TJPEGImage.Create;
    try
      BMP := TBitmap.Create;
      try
        BMP.LoadFromFile('C:\1.bmp');
        JPG.Assign(BMP);
      finally
        BMP.Free;
      end;
      JPG.CompressionQuality := 80;
      JPG.Compress;
      JPG.SaveToStream(MS);
    finally
      JPG.Free;
    end;
    MS.Position := 0;
    S := TIdEncoderMIME.EncodeStream(MS);
  finally
    MS.free;
  end;
  MyTCPSocket.SendString(S);
end;
uses
  ..., IdCoderMIME;

var
  S: string;
  MS: TMemoryStream;
begin
  S := MyTCPSocket.ReadString;
  MS := TMemoryStream.Create;
  try
    TIdDecoderMIME.DecodeStream(S, MS);
    MS.Position := 0;
    MS.SaveToFile('C:\2.jpg');
  finally
    MS.Free;
  end;
end;