Delphi Pages Forums  

Go Back   Delphi Pages Forums > Delphi Forum > General

Lost Password?

Closed Thread
 
Thread Tools Display Modes
  #1  
Old 08-02-2019, 10:16 PM
FreakaZoid2 FreakaZoid2 is offline
Senior Member
 
Join Date: Jul 2009
Posts: 361
Default Convert C# base64 string back to an image.

I am trying to take a C# base64 string and convert it back to an image. But everything I try fails with invalid bitmap image. Does the C# have something different about its encoding or am I going about this wrong...
Third party software and there security is tight so I only have limited information available but I have the complete string 4262 bytes long.
here is the beginning sample of it.

0x4749463839614B016700F700000000000000330000660000 990000CC0000FF003300003333

C# Code:
Code:
 
activity.SignatureData = pOptions.PaymentProcessorResult.Root?.Element("signatureData") != null ? Convert.FromBase64String(pOptions.PaymentProcessorResult.Root?.Element("signatureData")?.Value ?? string.Empty) : null;
my Delphi code:
Code:
procedure TForm1.btn1Click(Sender: TObject);
var
  s : string;
  bytes : TBytes;
  Encoding : TEncoding;
begin
  bytes := TNetEncoding.Base64.DecodeStringToBytes(mmo1.Lines.Text);
  s := TEncoding.ANSI.GetString(bytes);
  img1.Picture.Bitmap.Free;
  img1.Picture.Bitmap := BitmapFromBase64(s);
  img1.Picture.Bitmap.SaveToFile('C:\tmp\test.bmp');
end;
  #2  
Old 08-05-2019, 04:03 PM
FreakaZoid2 FreakaZoid2 is offline
Senior Member
 
Join Date: Jul 2009
Posts: 361
Default I got someone from our C# dev to show me the code for retrieving the image

Code:
if (activity.HasSignature)
{
var imageStream = new MemoryStream(activity.SignatureData);
    fieldSignature.Image = Image.FromStream(imageStream);
    fieldSignature.SizeMode = PictureBoxSizeMode.Zoom;
    imageStream.Dispose();
 
return;
}
This C# code work with the string I was given. not sure why the base64 part has no bearing.
  #3  
Old 08-05-2019, 07:56 PM
FreakaZoid2 FreakaZoid2 is offline
Senior Member
 
Join Date: Jul 2009
Posts: 361
Default OK more info as i go thru this

Don't think the base64 is in play. the 0x47 at the start of the string represents the start of a graphic format.

so how would I go about loading the string into a bitmap.
I am assuming that I have to change the string into bitmap format (binary?).
  #4  
Old 08-06-2019, 07:02 AM
Norrit Norrit is offline
Moderator
 
Join Date: Aug 2001
Location: Landgraaf
Posts: 7,333
Default

Are you sure the input is a Bitmap?

I thought 0x47 is a .PNG representation...
  #5  
Old 08-06-2019, 04:00 PM
FreakaZoid2 FreakaZoid2 is offline
Senior Member
 
Join Date: Jul 2009
Posts: 361
Default yeah i saw that in some forums

when I asked the C# dev what kind of image they were loading into they said it was a bitmap. maybe C# is doing the conversion or they are somewhere else in the background.
with that said I have tried loading that string into a via memorystream into a tcximage that has different graphics class types. png being one. nothing I change about the image helps so I figure is has something to do with the loading of the string.
  #6  
Old 08-06-2019, 04:11 PM
FreakaZoid2 FreakaZoid2 is offline
Senior Member
 
Join Date: Jul 2009
Posts: 361
Default Working with C# dev person

we have determined that the image( a signature) is converted to base64
then we take that and run the convert.base64string and store that string.
now I am trying to take the string and convert it back into an image that I can use. preferably bitmap.
i haven't been able to determine how the C# app is just pulling it into a memorystream and loading to image (bitmap presumably)
nothing i try works, SO far.
not sure why the string has the 0x47 at the beginning if it is supposed to base64...
  #7  
Old 08-07-2019, 07:55 AM
Norrit Norrit is offline
Moderator
 
Join Date: Aug 2001
Location: Landgraaf
Posts: 7,333
Default

What is the code in your BitmapFromBase64 method?

Because converting to bitmap you need to load it in the correct class (jpg, bmp, png), at least in the older Delphi versions
Normally this is done by the extension, but since you don't receive it I'm curious how you resolved this...
  #8  
Old 08-07-2019, 02:35 PM
FreakaZoid2 FreakaZoid2 is offline
Senior Member
 
Join Date: Jul 2009
Posts: 361
Default

Code:
function BitmapFromBase64(const base64: string): TBitmap;
var
  Input: TStringStream;
  Output: TBytesStream;
begin
  Input := TStringStream.Create(base64, TEncoding.ASCII);
  try
    Output := TBytesStream.Create;
    try
      Soap.EncdDecd.DecodeStream(Input, Output);
      Output.Position := 0;
      Result := TBitmap.Create;
      try
        Result.LoadFromStream(Output);
      except
        Result.Free;
        raise;
      end;
    finally
      Output.Free;
    end;
  finally
    Input.Free;
  end;
end;
  #9  
Old 08-07-2019, 02:39 PM
FreakaZoid2 FreakaZoid2 is offline
Senior Member
 
Join Date: Jul 2009
Posts: 361
Default the more i look at this from my side (delphi)

I cannot see how that string is base64. not with the 0x47 part at the beginning. So after some thought last night I am thinking that I need to take the string and turn it into binary. I get limited info from the C# dev team but today I will follow there logic all the way thru so I can get an idea about what they are really doing with that string.
  #10  
Old 08-07-2019, 02:46 PM
FreakaZoid2 FreakaZoid2 is offline
Senior Member
 
Join Date: Jul 2009
Posts: 361
Default

I have verified from Ingenico documentation that it is supposed to be a bitmap format.
Closed Thread

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On

Forum Jump


All times are GMT. The time now is 07:20 AM.


Powered by vBulletin® Version 3.8.8
Copyright ©2000 - 2019, vBulletin Solutions, Inc.