I would like to send a bitmap image with an HTTP POST method. How can I send it to a URL?
I am using Indy 10 and Delphi 10.1. In a procedure, I create a TStringList with all parameter values, but I don't know how to pass the bitmap data.
This is my code:
procedure TuDm_Athlos.AddComandaInsertLogo(workList: TStringList;
imageStream: TStream);
var
image : TBitmap;
begin
try
image := TBitmap.Create;
imageStream := TStream.Create;
image.LoadFromFile('D:\\COFEE.BMP');
image.SaveToStream(imageStream);
workList.Add('db=titles');
workList.Add('line_1=');
worklist.Add('line_2=');
workList.Add('line_3=');
workList.Add('line_4=');
workList.Add('line_5=');
workList.Add('line_6=');
workList.Add('store=&DB=PRN_UDG');
workList.Add('code=1');
workList.Add('width=' + IntToStr(image.Width));
workList.Add('height=' + IntToStr(image.Height));
workList.Add('length=576');
workList.Add('store=');
finally
FreeAndNil(imageStream);
end;
end;
function TuDm_Athlos.InsertLogo(imageStream: TStream;
isFullResponse: Boolean): Boolean;
var
StrResult : UTF8String;
workList : TStringList;
ContentStream : TStream;
image : TBitmap;
begin
//Setup;
Result := False;
try
try
workList := TStringList.Create;
ContentStream := TStream.Create;
image := TBitmap.Create;
image.LoadFromStream(imageStream);
AddComandaInsertLogo(workList,imageStream);
AddComandaSummarize(workList, False);
StrResult := IdHTTP1.Post(printerURL + 'db_status.xml?',workList);
ContentStream := StringToStream(strResult);
Result := XmlReadCommanda(imageStream); //XmlReadComanda(ContentStream);
except
on e : Exception do begin
//DisconnectHttpClient;
//raise Exception.Create(TranslateHttpError(e.Message));
end;
end;
finally
FreeAndNil(workList);
FreeAndNil(image);
ContentStream.Free;
end;
end;
You can send a file or stream using TIdMultiPartFormDataStream instead of TStringList.
uses
..., IdMultipartFormData;
procedure postImage(Url, FileName: String; imageStream: TStream);
var
Form : TIdMultiPartFormDataStream;
LStream: TStream;
begin
if imageStream = nil then
LStream := TIdReadFileExclusiveStream.Create(FileName)
else
LStream := imageStream;
try
Form := TIdMultiPartFormDataStream.Create;
try
Form.AddFormField('db', 'titles');
Form.AddFormField('line_1', '');
Form.AddFormField('line_2', '');
Form.AddFormField('line_3', '');
Form.AddFormField('line_4', '');
Form.AddFormField('line_5', '');
Form.AddFormField('line_6', '');
Form.AddFormField('store', '');
Form.AddFormField('DB', 'PRN_UDG');
Form.AddFormField('code','1');
Form.AddFormField('width', IntToStr(image.Width));
Form.AddFormField('height',IntToStr(image.Height));
Form.AddFormField('length','576');
Form.AddFormField('store','');
//CREATE A FIELD AND SET THE STREAM
Form.AddFormField('bitmap', '', '', LStream, FileName);
IdHTTP1.Post(Url, Form);
finally
Form.Free;
end;
finally
if imageStream = nil then
LStream.Free;
end;
end;
procedure postImage(Url, FileName : String);
begin
postImage(Url, FileName, nil);
end;
Related
i have this Thread that get image url from the web then save it to memory stream then save from memory stream To file
i needed to convert any image that downloaded to a gif image so i do something like this
unit downloadimgThread;
interface
uses Windows, SysUtils, Classes, dialogs, IdSSLOpenSSL, IdHttp, IdUri, System.AnsiStrings, Graphics, Jpeg, Vcl.Imaging.GIFImg, PNGImage;
type
TDownloadUpdateVisualEvent = procedure(Sender: TObject; Anameofimg: String;
var Aimagelocate: String) of object;
type
TURLDownload = class(TThread)
private
FOnUpdateVisual: TDownloadUpdateVisualEvent;
FURL: String;
Fnameofimg: string;
FPathImage: string;
FFileNameImage: string;
ImageName: string;
PathURL: string;
procedure DoUpdateVisual;
protected
procedure Execute; override;
public
constructor Create(Thrdid: Pointer; const AUrl: String;
Const AOutPathImages: string; AOnUpdateVisual: TDownloadUpdateVisualEvent;
Anameofimg: String); reintroduce;
property URL: string read FURL write FURL;
property PathImage: string read FPathImage;
property FileNameImage: string read FFileNameImage;
end;
var
URLDOWNLOAD: TURLDownload;
implementation
{ TURLDownload }
function JpgToGif(ms: TMemoryStream): Boolean;
var
gif: TGIFImage;
jpg: TJPEGImage;
begin
Result := False;
gif := TGIFImage.Create;
try
jpg := TJPEGImage.Create;
try
//jpg
ms.Position := 0;
jpg.LoadFromStream(ms);
jpg.DIBNeeded;
gif.Assign(jpg);
//save...
ms.Clear;
gif.SaveToStream(ms);
Result := True;
finally
jpg.Free;
jpg := nil;
end;
finally
gif.Free;
gif := nil;
end;
end;
constructor TURLDownload.Create(Thrdid: Pointer; const AUrl, AOutPathImages: string; AOnUpdateVisual: TDownloadUpdateVisualEvent; Anameofimg: String);
var
URI: TIdURI;
begin
inherited Create(false);
FreeOnTerminate := True;
FURL := AUrl;
FOnUpdateVisual := AOnUpdateVisual;
Fnameofimg := Anameofimg;
FPathImage := AOutPathImages;
URI := TIdURI.Create(AUrl);
try
ImageName := URI.Document;
PathURL := URI.path;
finally
URI.Free;
end;
end;
procedure TURLDownload.DoUpdateVisual;
begin
if Assigned(FOnUpdateVisual) then
FOnUpdateVisual(self, Fnameofimg, FFileNameImage);
end;
procedure TURLDownload.Execute;
var
aMs: TMemoryStream;
aIdHttp: TIdHttp;
IdSSL: TIdSSLIOHandlerSocketOpenSSL;
path: string;
dir: string;
SPEXT : String;
itsimage: string;
responsechk: Integer;
begin
dir := AnsiReplaceText(PathURL, '/', '');
if (ImageName = '') then
begin
exit;
end;
SPEXT := ExtractFileExt(ImageName);
ImageName := Copy(ImageName, 1, Length(ImageName) - Length(SPEXT));
path := PathImage + '\' + ImageName + '.gif';
if fileexists(path) then
begin
FFileNameImage := path;
if Assigned(FOnUpdateVisual) then
begin
Synchronize(DoUpdateVisual);
end;
exit;
end
else
if not fileexists(path) then
begin
aMs := TMemoryStream.Create;
aIdHttp := TIdHttp.Create(nil);
IdSSL := TIdSSLIOHandlerSocketOpenSSL.Create(nil);
try
IdSSL.SSLOptions.Method := sslvTLSv1;
IdSSL.SSLOptions.Mode := sslmUnassigned;
aIdHttp.HTTPOptions := [hoForceEncodeParams] + [hoNoProtocolErrorException];
aIdHttp.IOHandler := IdSSL;
aIdHttp.AllowCookies := True;
aIdHttp.Request.UserAgent := 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:12.0) Gecko/20100101 Firefox/12.0';
aIdHttp.HandleRedirects := True;
aIdHttp.RedirectMaximum := 3;
try
aIdHttp.Head(trim(FURL));
except
end;
itsimage := aIdHttp.Response.ContentType;
responsechk := aIdHttp.ResponseCode;
if responsechk <> 200 then
begin
FFileNameImage := 'error';
if Assigned(FOnUpdateVisual) then
begin
Synchronize(DoUpdateVisual);
end;
exit;
end;
if (itsimage = 'image/gif') then
begin
try
aIdHttp.Get(trim(FURL), aMs);
except
end;
aMs.SaveToFile(path);
end else if (itsimage = 'image/jpeg') then
begin
try
aIdHttp.Get(trim(FURL), aMs);
except
end;
if JpgToGif(aMs) then
begin
aMs.SaveToFile(path);
end;
end;
try
if aIdHttp.Connected then
aIdHttp.Disconnect;
except
end;
finally
aMs.Free;
IdSSL.Free;
aIdHttp.Free;
end;
end;
FFileNameImage := path;
if Assigned(FOnUpdateVisual) then
begin
Synchronize(DoUpdateVisual);
end;
end;
end.
in this unit i try to check if image type is jpg then convert it to gif and save it specifically at this line of code
if (itsimage = 'image/jpeg') then
begin
try
aIdHttp.Get(trim(FURL), aMs);
except
end;
if JpgToGif(aMs) then
begin
aMs.SaveToFile(path);
end;
// function to convert
function JpgToGif(ms: TMemoryStream): Boolean;
var
gif: TGIFImage;
jpg: TJPEGImage;
begin
Result := False;
gif := TGIFImage.Create;
try
jpg := TJPEGImage.Create;
try
//jpg
ms.Position := 0;
jpg.LoadFromStream(ms);
jpg.DIBNeeded;
gif.Assign(jpg);
//save...
ms.Clear;
gif.SaveToStream(ms);
Result := True;
finally
jpg.Free;
jpg := nil;
end;
finally
gif.Free;
gif := nil;
end;
end;
when i try to convert the image and save it the image saved is corrupted what could be the issue ?
There is a very simple solution for this. And that is to use FMX Bitmap instead of default VCL Bitmap as it allows automatic format recognition on load and automatic format choosing on save based on file extension of the file name you provide to SaveToFile method.
Here is a simple code that loads selected image chosen in OpenDialog into Memory stream first and then into Bitmap and then it saves the image into GIF format.
procedure TForm1.Button1Click(Sender: TObject);
var Bitmap: FMX.Graphics.TBitmap;
MS: TMemoryStream;
begin
if OpenDialog1.Execute then
begin
MS := TMemoryStream.Create;
MS.LoadFromFile(OpenDialog1.FileName);
Bitmap := FMX.Graphics.TBitmap.Create;
Bitmap.LoadFromStream(MS);
Bitmap.SaveToFile('D:\Proba.gif');
end;
end;
As you can see you only need just a few lines and you get ability to convert images between all supported formats.
You can see which ones are supported here:
http://docwiki.embarcadero.com/Libraries/XE8/en/FMX.Graphics.TBitmapCodecManager#Supported_Image_Formats
Just make sure you are indeed using FMX.Graphics.TBitmap by specifying the full namespace for the file in which it resided.
NOTE: Working on VCL application does not mean you can't use some of the functionality that is present in Fire Monkey.
I need to access binary image data using XMLHttpRequest in Delphi. I am using the following code but its not working, could someone please tell me what's wrong with this code, thanks in advance.
//I am using this function to get Image Binary data into Memory Stream.
procedure SendGETRequest(p_Url: string; p_resStream: TMemoryStream);
begin
FXmlHttpReq.open(METHOD_GET, p_Url, false, FUsername, FPassword);
FXmlHttpReq.setRequestHeader(HTTP_AUTHENTICATION, HTTP_BASIC + EncodeBase64(
FUsername + ':'+FPassword));
FXmlHttpReq.setRequestHeader(HTTP_CACHE_CONTROL, HTTP_NO_CACHE);
//FXmlHttpReq.setRequestHeader('Content-type','application/octet-stream');
FXmlHttpReq.send('');
if not VarIsEmpty(FXmlHttpReq.responseBody) then
begin
p_resStream:= OleVariantToMemoryStream(FXmlHttpReq.responseStream);
end;//if...
end;
function OleVariantToMemoryStream(OV: OleVariant): TMemoryStream;
var
Data: PByteArray;
Size: integer;
begin
Result := TMemoryStream.Create;
try
Size := VarArrayHighBound (OV, 1) - VarArrayLowBound(OV, 1) + 1;
Data := VarArrayLock(OV);
try
Result.Position := 0;
Result.WriteBuffer(Data^, Size);
finally
VarArrayUnlock(OV);
end;
except
Result.Free;
Result := nil;
end;
end;
responseStream is IStream. You need to convert it using TOleStream (AxCtrls):
uses AxCtrls, ComObj, ActiveX;
procedure TForm1.Button1Click(Sender: TObject);
var
oXMLHTTP: OleVariant;
MemoryStream: TMemoryStream;
Stream: IStream;
OleStream: TOleStream;
begin
oXMLHTTP := CreateOleObject('MSXML2.XMLHTTP.3.0');
oXMLHTTP.open('GET', 'https://www.google.com/images/srpr/logo11w.png', False);
oXMLHTTP.send(EmptyParam);
Stream := IUnknown(oXMLHTTP.ResponseStream) as IStream;
OleStream := TOleStream.Create(Stream);
try
OleStream.Position := 0;
MemoryStream := TMemoryStream.Create;
try
MemoryStream.CopyFrom(OleStream, OleStream.Size);
MemoryStream.SaveToFile('logo11w.png');
finally
MemoryStream.Free;
end;
finally
OleStream.Free;
end;
end;
I have a Stored Procedure that takes blob data (VarBinary) as a parameter I can't get it to work with TADOStoredProc though.
The Stored Procedure
ALTER PROCEDURE [dbo].[spAddToSolve] #SolveData VarBinary(max)
AS
BEGIN
INSERT INTO dbo.ToSolve (Data, SolveStatus)
VALUES (#SolveData, 0)
END
Here are the two things I tried.
This first one fails finding the Field, what am I Missing
procedure AddItem(dbCon : TADOConnection; sourcePath : String);
var
addProc : TADOStoredProc;
field : TField;
dataStream : TFileStream;
blobStream : TStream;
begin
if FileExists(sourcePath) then
begin
dataStream := TFileStream.Create(sourcePath, fmOpenREad or fmShareDenyNone);
try
addProc := TADOStoredProc.Create(nil);
addProc.Connection := dbCon;
addProc.ProcedureName := 'spAddToSolve';
field := addProc.FieldByName('#SolveData'); //Field'#SolveData' not found
blobStream := addProc.CreateBlobStream(field, bmWrite);
blobStream.CopyFrom(dataStream, dataStream.Size);
addProc.Open;
finally
addProc.Free();
dataStream.Free();
end;
end;
end;
Not sure how to get the method to work, I don't know how to get the stream data into the param value.
procedure AddItem(dbCon : TADOConnection; sourcePath : String);
var
addProc : TADOStoredProc;
param : TParameter;
field : TField;
dataStream : TFileStream;
begin
if FileExists(sourcePath) then
begin
dataStream := TFileStream.Create(sourcePath, fmOpenREad or fmShareDenyNone);
try
addProc := TADOStoredProc.Create(nil);
addProc.Connection := dbCon;
addProc.ProcedureName := 'spAddToSolve';
param := addProc.Parameters.AddParameter;
param.Name := '#SolveData';
param.DataType := ftBlob;
//not sure what to do next
param.Value := dataStream; //Types are not compatable
addProc.Open;
finally
addProc.Free();
dataStream.Free();
end;
end;
end;
Your second try is on the right track. This should work:
param.DataType := ftBlob;
param.LoadFromStream(dataStream);
I try to insert into blob field in SQLite with Delphi XE3.
I've been using TDBXCommand and TSQLConnection like this.
but blob field is not inserted and even i cannnot get any result from query
procedure TDBXCommandHelper.Init(const AQry: String);
begin
Parameters.ClearParameters;
Close;
Prepare;
Text := AQry;
end;
procedure dmDB.InsertPicture;
const
QRY = 'INSERT INTO Memo(Picture) VALUES(?)';
var
LTransaction: TDBXTransaction;
LBlob: TDBXParameter;
LStream: TFileStream;
begin
LTransaction := FDBCon.BeginTransaction;
LStream := TFileStream.Create('d:\sample.bmp', fmOpenRead);
LBlob := TDBXParameter.Create;
try
try
FDBXCmd := FDBCon.DBXConnection.CreateCommand;
FDBXCmd.CommandType := TDBXCommandTypes.DbxSQL;
FDBXCmd.Init(QRY);
LBlob.DataType := TDBXDataTypes.BlobType;
LBlob.SubType := TDBXSubDataTypes.BinarySubType;
LBlob.Value.SetStream(LStream, False);
FDBXCmd.Parameters.AddParameter(LBlob);
FDBXCmd.ExecuteUpdate;
except
on E: Exception do
FDBCon.RollbackFreeAndNil(LTransaction);
end;
FDBCon.CommitFreeAndNil(LTransaction);
finally
FreeAndNil(LStream);
FreeAndNil(LBlob);
end;
end;
using TSQLConnection but i cannot get any result
procedure TInsertThread.NoteInsertExcute;
const
QRY = 'INSERT INTO Memo(Picture) VALUES(:Picture)';
var
LTransaction: TDBXTransaction;
LParams: TParams;
LStream: TMemoryStream;
begin
LTransaction := FDBCon.BeginTransaction;
LParams := TParams.Create(nil);
LStream := TMemoryStream.Create;
LStream.LoadFromFile(FValues.Values[NAME_PICTURE]);
try
LParams.CreateParam(ftBlob, 'Picture', ptInput);
LParams.ParamByName('Picture').LoadFromStream(LStream, ftBlob);
FDBCon.Execute(QRY, LParams);
FDBCon.CommitFreeAndNil(LTransaction);
finally
FreeAndNil(LStream);
FreeAndNil(LParams);
end;
end;
That's easy like:
var
ms: TMemoryStream;
sq: TSQLQuery;
begin
ms := TMemoryStream.Create;
ms.LoadFromFile('C:\Pictures\l.jpg');
if ms <> nil then
begin
sq := TSQLQuery.Create(nil);
sq.SQLConnection := con1;
sq.SQL.Text := 'update db1 set picture= :photo ;';
sq.Params.ParseSQL(sq.SQL.Text, true);
sq.Params.ParamByName('photo').LoadFromStream(ms, ftBlob);
sq.ExecSQL();
end;
.
Where con1 is a TSQLConnection.
You can try the following:
function GetFileAsBytesValue(AFileName: TFileName): TArray<Byte>;
var
Len: Integer;
LStream: TMemoryStream;
begin
LStream := TMemoryStream.Create;
try
LStream.LoadFromFile(AFileName);
Len := LStream.Size;
SetLength(Result, Len);
Move(LStream.Memory^, Result[0], Len);
finally
LStream.Free;
end;
end;
procedure dmDB.InsertPicture;
const
QRY = 'INSERT INTO Memo(Picture) VALUES(?)';
var
LTransaction: TDBXTransaction;
LDBXCmd: TSQLQuery;
LParam: TParam;
begin
LTransaction := FDBCon.BeginTransaction;
LDBXCmd := TSQLQuery.Create(FDBCon);
try
try
LDBXCmd.SQLConnection := FDBCon;
LDBXCmd.SQL.Text := QRY;
LParam := LDBXCmd.Params.CreateParam(ftBlob, 'Picture', ptInput);
LParam.AsBlob := GetFileAsBytesValue('d:\sample.bmp');
LDBXCmd.ExecSQL;
except
on E: Exception do
FDBCon.RollbackFreeAndNil(LTransaction);
end;
FDBCon.CommitFreeAndNil(LTransaction);
finally
LDBXCmd.Free;
end;
end;
I am trying to post data to bing with this code
function PostExample: string;
var
lHTTP: TIdHTTP;
lParamList: TStringList;
begin
lParamList := TStringList.Create;
lParamList.Add('q=test');
lHTTP := TIdHTTP.Create(nil);
try
Result := lHTTP.Post('http://www.bing.com/', lParamList);
finally
FreeAndNil(lHTTP);
FreeAndNil(lParamList);
end;
end;
And then, how can I get the result to the TWebBrowser and display it?
Try LoadDocFromString:
procedure LoadBlankDoc(ABrowser: TWebBrowser);
begin
ABrowser.Navigate('about:blank');
while ABrowser.ReadyState <> READYSTATE_COMPLETE do
begin
Application.ProcessMessages;
Sleep(0);
end;
end;
procedure CheckDocReady(ABrowser: TWebBrowser);
begin
if not Assigned(ABrowser.Document) then
LoadBlankDoc(ABrowser);
end;
procedure LoadDocFromString(ABrowser: TWebBrowser; const HTMLString: wideString);
var
v: OleVariant;
HTMLDocument: IHTMLDocument2;
begin
CheckDocReady(ABrowser);
HTMLDocument := ABrowser.Document as IHTMLDocument2;
v := VarArrayCreate([0, 0], varVariant);
v[0] := HTMLString;
HTMLDocument.Write(PSafeArray(TVarData(v).VArray));
HTMLDocument.Close;
end;
Or you can use the memory streams for loading
uses
OleCtrls, SHDocVw, IdHTTP, ActiveX;
function PostRequest(const AURL: string; const AParams: TStringList;
const AWebBrowser: TWebBrowser): Boolean;
var
IdHTTP: TIdHTTP;
Response: TMemoryStream;
begin
Result := True;
try
AWebBrowser.Navigate('about:blank');
while AWebBrowser.ReadyState < READYSTATE_COMPLETE do
Application.ProcessMessages;
Response := TMemoryStream.Create;
try
IdHTTP := TIdHTTP.Create(nil);
try
IdHTTP.Post(AURL, AParams, Response);
if Response.Size > 0 then
begin
Response.Position := 0;
(AWebBrowser.Document as IPersistStreamInit).Load(
TStreamAdapter.Create(Response, soReference));
end;
finally
IdHTTP.Free;
end;
finally
Response.Free;
end;
except
Result := False;
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
Params: TStringList;
begin
Params := TStringList.Create;
try
Params.Add('q=test');
if not PostRequest('http://www.bing.com/', Params, WebBrowser1) then
ShowMessage('An unexpected error occured!');
finally
Params.Free;
end;
end;