Delphi Pages Forums  

Go Back   Delphi Pages Forums > Delphi Forum > DB Tools

Lost Password?

Reply
 
Thread Tools Display Modes
  #1  
Old 12-31-2016, 03:50 PM
kbboykin kbboykin is offline
Senior Member
 
Join Date: May 2005
Posts: 365
Default Why does this give JPEG error #42 when is in file

procedure TfrmWines.FormActivate(Sender: TObject);
var
Field: TBlobField;
Stream: TStream;
Jpg: TJPEGImage;
begin
if wdatamod.mywines.Active then
begin
Field := TBlobField(wdatamod.mywines.FieldByName('winelabel '));
Stream := wdatamod.mywines.CreateBlobStream(Field, bmRead);
Jpg := TJPEGImage.Create;
try
Jpg.LoadFromStream(Stream);
DBImage1.Picture.Graphic := Jpg;
finally
Jpg.Free;
Stream.Free;
end;
end;
end;
__________________
Using Delphi 2010 Professional as a Novice Programmer - self taught
Reply With Quote
  #2  
Old 12-31-2016, 09:50 PM
GeoWink GeoWink is offline
Senior Member
 
Join Date: Jan 2001
Posts: 879
Default

There are quite a few discussions about this error on Google. Check it out. They say it is because the file does not get loaded properly.

George
Reply With Quote
  #3  
Old 01-02-2017, 01:37 AM
kbboykin kbboykin is offline
Senior Member
 
Join Date: May 2005
Posts: 365
Default

Yes, I have tried and looked at all these examples, ad naseum, and still can't get this to fly. There has got to be a way in Delphi 2010 to get this to work without exceptions.
__________________
Using Delphi 2010 Professional as a Novice Programmer - self taught
Reply With Quote
  #4  
Old 01-02-2017, 03:50 PM
kbboykin kbboykin is offline
Senior Member
 
Join Date: May 2005
Posts: 365
Default

I have figured out how to at least store and retrieve the jpeg files in the database. But if the blobfield is empty, I always get jpeg #42 error until I load an image in the field. I can edit the database just fine, but when I try to add a new record I'm back to JPEG Error #42 again. I can go ahead and enter a record by bypassing the pop, but it's annoying. Here's my code to show images and insert a new record


Code:
procedure TfrmWines.AutoSetDataChange(Sender: TObject; Field: TField);
var
  JPG:TJPEGImage;
  ms:TMemoryStream;
begin
  begin
    JPG:=TJPEGImage.Create;
    ms:=TMemoryStream.Create;
    try
      TBlobField(wdatamod.mywines.FieldByName('winelabel')).SaveToStream(ms);
      ms.Position := 0;
      JPG.LoadFromStream(ms);
      Image1.Picture.Assign(JPG);
   finally
     JPG.Free;
     ms.Free;
   end;
  end;
end;

procedure TfrmWines.btnAddClick(Sender: TObject);
begin
  ButtonsEnter;
  //btnLabelGet.Click;
  wdatamod.mywines.Insert;
  edWinename.SetFocus;
end;

procedure TfrmWines.btnLabelGetClick(Sender: TObject);
begin
  if OpenPictureDialog1.Execute(Self.Handle) then
  Image1.Picture.LoadFromFile(OpenPictureDialog1.FileName);
end;

procedure TfrmWines.btnSaveClick(Sender: TObject);
var
  AStream : TMemoryStream;
begin
  AStream := TMemoryStream.Create;
  try
    Image1.Picture.Graphic.SaveToStream(AStream);
    AStream.Position := 0;
    if wdatamod.mywines.Active then
    begin
      TBlobField(wdatamod.mywines.FieldByName('winelabel')).LoadFromStream(AStream);
    end;
  finally
    AStream.Free;
  end;
  wdatamod.mywines.Post;
  ButtonsNormal;
end;
The btnLabelGetClick code is used to get the image to be stored while either adding or editing a record.

Can someone offer some insight. How can I get past an empty blobfield on inserting new record? How can I allow it to not save an image if don't want to.?

Thanks in advance.
__________________
Using Delphi 2010 Professional as a Novice Programmer - self taught
Reply With Quote
  #5  
Old 01-02-2017, 09:50 PM
kbboykin kbboykin is offline
Senior Member
 
Join Date: May 2005
Posts: 365
Default Solved

I solved this and found the way to add a standard image to the database when I don't have and image. I only show the code snippet that fixed what I wanted below

Code:
procedure TfrmWines.AutoSetDataChange(Sender: TObject; Field: TField);
var
  JPG:TJPEGImage;
  ms:TMemoryStream;
begin
  if autoset.DataSet.State = dsInsert then
  begin
    image1.Picture.Graphic.LoadFromFile('q:\sourcecode\mycellar\images\blk_wht_glass.jpg');
    Exit;
  end;
  begin
    JPG:=TJPEGImage.Create;
    ms:=TMemoryStream.Create;
    try
      TBlobField(wdatamod.mywines.FieldByName('winelabel')).SaveToStream(ms);
      ms.Position := 0;
      JPG.LoadFromStream(ms);
      Image1.Picture.Assign(JPG);
   finally
     JPG.Free;
     ms.Free;
   end;
  end;
end;
__________________
Using Delphi 2010 Professional as a Novice Programmer - self taught
Reply With Quote
Reply

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 Off

Forum Jump


All times are GMT. The time now is 03:09 AM.


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