Ver Mensaje Individual
  #4  
Antiguo 29-08-2010
Avatar de TheJHorse
TheJHorse TheJHorse is offline
Miembro
 
Registrado: dic 2005
Posts: 13
Reputación: 0
TheJHorse Va por buen camino
thejhorse's solution

Un poco tarde tal vez la solucion, pero no deseo dejar este problema sin alguna opcion para resolverlo.

En SQL Server 2000
Estructura de la base de datos "db_prueba":
Código SQL [-]
CREATE TABLE tbl_prueba
(
  codigo INT IDENTITY(1,1) NOT NULL PRIMARY KEY,
  datos  IMAGE
)

un Store procedure, para almacenar datos:
Código SQL [-]
CREATE PROCEDURE spNuevaImagen
  @imgData  IMAGE,
  @iRetorno INT OUTPUT
AS
  INSERT INTO tbl_prueba (datos) VALUES (@imgData)

  SET @iRetorno = @@IDENTITY
GO

En la aplicacion Delphi
Usamos:
Código Delphi [-]
// Agregar DBTables, DB

acConeccion: TADOConnection;
spProcedimiento: TADOStoredProc;
txtImagenTif: TEdit;
btnGuardarImagen: TButton;;
Image1: TImage;
btnCargarImagen: TButton;

Para almacenar la imagen
Código Delphi [-]
procedure TForm1.btnGuardarImagenClick(Sender: TObject);
var
  iResultado: Integer;
  CampoBlob : TStream;
begin
  btnGuardarImagen.Enabled := False;

  // Cambiar Nombre de usuario de la base de datos, su password, servidor SQL
  acConeccion.ConnectionString := 'Provider=SQLNCLI.1;Persist Security Info=False;User ID=NOMBREUSUARIO;Password=TUPASSWORD;Initial Catalog=db_prueba;Data Source=.';

  try
    acConeccion.Open();
  except
    Application.Terminate();
  end;

  spProcedimiento.Close;
  spProcedimiento.Prepared := false;
  spProcedimiento.ProcedureName := 'spNuevaImagen';
  spProcedimiento.Parameters.Refresh;

  spProcedimiento.Parameters.ParamByName('@imgData').LoadFromFile(txtImagenTif.Text, ftBlob);
  spProcedimiento.Parameters.ParamByName('@iRetorno').Value := 0;
  spProcedimiento.Prepared := true;
  spProcedimiento.ExecProc();

  // iResultado almacena el codigo que se creo
  iResultado := spProcedimiento.Parameters.ParamByName('@iRetorno').Value;

  acConeccion.Close();

  btnGuardarImagen.Enabled := True;
end;

Para cargar la imagen:
Código Delphi [-]
procedure TForm1.btnCargarImagenClick(Sender: TObject);
var
  rsDocumentos: TADOQuery;
  blob: TStream;
begin
  // Cambiar Nombre de usuario de la base de datos, su password, servidor SQL
  acConeccion.ConnectionString := 'Provider=SQLNCLI.1;Persist Security Info=False;User ID=NOMBREUSUARIO;Password=TUPASSWORD;Initial Catalog=db_prueba;Data Source=.';

  try
    acConeccion.Open();
  except
    Application.Terminate();
  end;

  rsDocumentos := TADOQuery.Create(Self);

  rsDocumentos.Connection := acConeccion;
  rsDocumentos.Active := False;
  rsDocumentos.SQL.Text := ' SELECT * FROM tbl_prueba WHERE codigo = 1';
  rsDocumentos.Active := True;

  rsDocumentos.First();

  while (not rsDocumentos.Eof) do
  begin
    blob := rsDocumentos.CreateBlobStream(rsDocumentos.FieldByName('datos'), bmRead);
    try
      // Mostramos la imagen
      blob.Seek(0, soFromBeginning);
      Image1.Picture.Graphic.LoadFromStream(blob);

      // Guardamos los datos en disco
      blob.Seek(0, soFromBeginning);

      with TFileStream.Create('c:\archivox.jpg', fmCreate) do
      try
        CopyFrom(blob, blob.Size)
      finally
        Free
      end;
    finally
      blob.Free
    end;

    rsDocumentos.Next();
  end;

  rsDocumentos.Close();
  rsDocumentos.Free();
  acConeccion.Close();
end;

Bueno si vas a usar imagenes JPG, ten en cuenta que debes cargar una imagen JPG en el control Image1, en tiempo de diseño, ya que si no lo haces te saldra error al cargar la imagen de Stream en run time.

Última edición por TheJHorse fecha: 29-08-2010 a las 19:07:33.
Responder Con Cita