#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2012
    Posts
    12
    Rep Power
    0

    LOAD,SAVE and RETRIVE PICTURE FROM MySql database USING DELPHI - HELP PLEASE !!!


    Hello,

    I need help with delphi project. I use delphi 2007 and MySql Database.

    1. I want to load picture from TOpenPictureDialog to TImage (or TDBImage).
    2. Picture from Timage (or TDBImage) I want to save to MySql database in table Picture field 'IMG'(longblob).
    3. I want to retrieve image from my database table 'IMG' and show in TImage (or TDBImage).
    4. I will have DBGrid on my main form. When I clik on GBgrid row or when I scroll GBGrid I want to see that picture are changing.
    5. How to convert jpg to gif, or how to Shrink picture or limit picture to be H:161px and W:138px, that is a size of my TImage.

    My database(MySql) have 4 table (contacts, city, country and picture).
    Database will be small maybe 200 contacts so I think that saving picture in database is not a problem.

    On my Main form I have DBGrid, TImage (or TDBImage), AddContact button, ChangeContact Button, DeleteContact Button,
    ChoosePicture Button, Save and Cancel Button, and DBEdit's from AdoDataSet,
    TAdoConnection,TAdoDataSet and DataSource, TOpenPictureDialog.

    This is my database:
    CREATE TABLE `picture` (
    `ID` int(10) unsigned NOT NULL auto_increment,
    `CAPTION` varchar(45) default NULL,
    `IMG` longblob,
    PRIMARY KEY (`ID`)
    ) ENGINE=InnoDB DEFAULT CHARSET=latin1;

    CREATE TABLE `country` (
    `ID` int(10) unsigned NOT NULL auto_increment,
    `NAME` varchar(45) NOT NULL,
    `AREACODE` varchar(45) default NULL,
    PRIMARY KEY (`ID`)
    ) ENGINE=InnoDB DEFAULT CHARSET=latin1;

    CREATE TABLE `city` (
    `ID` int(10) unsigned NOT NULL auto_increment,
    `NAME` varchar(45) NOT NULL,
    `ZIPCODE` int(5) unsigned default NULL,
    `COUNTRYID` int(10) unsigned default NULL,
    PRIMARY KEY (`ID`),
    KEY `FK_city_1` (`COUNTRYID`),
    CONSTRAINT `FK_city_1` FOREIGN KEY (`COUNTRYID`) REFERENCES `country` (`ID`)
    ) ENGINE=InnoDB DEFAULT CHARSET=latin1;

    CREATE TABLE `contacts` (
    `ID` int(10) unsigned NOT NULL auto_increment,
    `FIRSTNAME` varchar(45) NOT NULL,
    `LASTNAME` varchar(45) NOT NULL,
    `ADDRESS` varchar(45) default NULL,
    `PHONE` int(10) unsigned default NULL,
    `CITYID` int(10) unsigned default NULL,
    `PICID` int(10) unsigned default NULL,
    PRIMARY KEY (`ID`),
    KEY `FK_contacts_1` (`CITYID`),
    KEY `FK_contacts_2` (`PICID`),
    CONSTRAINT `FK_contacts_2` FOREIGN KEY (`PICID`) REFERENCES `picture` (`ID`),
    CONSTRAINT `FK_contacts_1` FOREIGN KEY (`CITYID`) REFERENCES `city` (`ID`)
    ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
    I have screenshot of my demo project .
    http://imageshack.us/photo/my-images/805/ttttttb.jpg/
    Thank you for your time and knowledge.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Jan 2006
    Location
    Carlsbad, CA
    Posts
    2,055
    Rep Power
    383
    Presumably you would use:
    TImage.Picture.SaveToStream
    and
    TImage.Picture.LoadFromStream

    I have not worked with MySQL, so I do not know the best syntax for transferring to and from MySQL BLOBs.

    Perhaps using TBlobField or perhaps using some custom MySQL component?

    HTH - Clive
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2008
    Posts
    344
    Rep Power
    6
    Maybe this may help.

    http://stackoverflow.com/a/4204076
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2012
    Posts
    12
    Rep Power
    0
    Hi clivew,

    I use TImage.Picture.SaveToStream and TImage.Picture.LoadFromStream.
    I can save picture in my database, but problem is when I want do retrieve
    picture and show in TImage.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2012
    Posts
    12
    Rep Power
    0
    Hi majlumbo,

    I Read post on http://stackoverflow.com/a/4204076, I can save picture in my database, but I cant retrieve picture and show on Timage.
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2012
    Posts
    12
    Rep Power
    0

    Need Example - Please.


    Can somebody post some small project example How to save a (Jpeg) image to MySql and retrieve it later using Delphi?
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2012
    Posts
    151
    Rep Power
    2
    I wrote a tutorial about this. Here is its url: http://forum.codecall.net/topic/67212-storing-images-in-database-mysql-and-ms-access-example/#entry617175

    Note that in the tutorial I was using Zeos library to work with MySQL server.
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2009
    Posts
    25
    Rep Power
    0

    MYSql pics


    I do this on a regular basis, but I never use Blobs etc. I simply put the local file address as a text field and retrieve this info into TImage as the the path to that pic. It is much faster this way.
    eg: in a field of you database, "mypics" i have the text stored as: 'C:\mypics\fish.jpg'. I retrieve this as a variable, eg: "thepic:= row[30];
    Now in Timage I simply retrieve this field data as: MyImage.Picture.LoadfromFile:= thepic;

IMN logo majestic logo threadwatch logo seochat tools logo