Einzelnen Beitrag anzeigen

EWeiss
(Gast)

n/a Beiträge
 
#5

AW: Memorystream vs TMemoryStream

  Alt 17. Apr 2015, 09:14
Danke für deine Ausführung.. Werde das mal in ruhe durchgehen.
Hier nur noch als Info wie das im eigenen Sample von Delphi gehandhabt wird.
Auf der Basis wollte ich das auch machen (was aber nun mal nicht geht mit den Streams in .NET)

Delphi-Quellcode:
procedure TForm1.Button5Click(Sender: TObject);
var
    PictureStream: TMemoryStream;
    Description: String;
    MIMEType: String;
    JPEGPicture: TJPEGImage;
    PNGPicture: TPNGImage;
    GIFPicture: TGIFImage;
    BMPPicture: TBitmap;
    Width, Height: Integer;
    NoOfColors: Integer;
    ColorDepth: Integer;
    PictureMagic: Word;
    CoverArtPictureFormat: TTagPictureFormat;
    CoverArtData: TCoverArtData;
begin
    if NOT OpenDialog1.Execute then begin
        Exit;
    end;
    //* Clear the cover art data
    MIMEType := '';
    Description := '';
    Width := 0;
    Height := 0;
    ColorDepth := 0;
    NoOfColors := 0;
    CoverArtPictureFormat := TTagPictureFormat.tpfUnknown;
    if FileExists(OpenDialog1.FileName) then begin
        try
            PictureStream := TMemoryStream.Create;
            try
                PictureStream.LoadFromFile(OpenDialog1.FileName);
                PictureStream.Seek(0, soBeginning);
                Description := ExtractFileName(OpenDialog1.FileName);
                PictureStream.Read(PictureMagic, 2);
                PictureStream.Seek(0, soBeginning);
                if PictureMagic = MAGIC_JPG then begin
                    MIMEType := 'image/jpeg';
                    CoverArtPictureFormat := tpfJPEG;
                    JPEGPicture := TJPEGImage.Create;
                    try
                        JPEGPicture.LoadFromStream(PictureStream);
                        Width := JPEGPicture.Width;
                        Height := JPEGPicture.Height;
                        NoOfColors := 0;
                        ColorDepth := 24;
                    finally
                        FreeAndNil(JPEGPicture);
                    end;
                end;
                if PictureMagic = MAGIC_PNG then begin
                    MIMEType := 'image/png';
                    CoverArtPictureFormat := tpfPNG;
                    PNGPicture := TPNGImage.Create;
                    try
                        PNGPicture.LoadFromStream(PictureStream);
                        Width := PNGPicture.Width;
                        Height := PNGPicture.Height;
                        NoOfColors := 0;
                        ColorDepth := PNGPicture.PixelInformation.Header.BitDepth;
                    finally
                        FreeAndNil(PNGPicture);
                    end;
                end;
                if PictureMagic = MAGIC_GIF then begin
                    MIMEType := 'image/gif';
                    CoverArtPictureFormat := tpfGIF;
                    GIFPicture := TGIFImage.Create;
                    try
                        GIFPicture.LoadFromStream(PictureStream);
                        Width := GIFPicture.Width;
                        Height := GIFPicture.Height;
                        NoOfColors := 0; //GIFPicture.ColorResolution
                        ColorDepth := GIFPicture.BitsPerPixel;
                    finally
                        FreeAndNil(GIFPicture);
                    end;
                end;
                if PictureMagic = MAGIC_BMP then begin
                    MIMEType := 'image/bmp';
                    CoverArtPictureFormat := tpfBMP;
                    BMPPicture := TBitmap.Create;
                    try
                        BMPPicture.LoadFromStream(PictureStream);
                        Width := BMPPicture.Width;
                        Height := BMPPicture.Height;
                        NoOfColors := 0;
                        case BMPPicture.PixelFormat of
                            pfDevice: ColorDepth := 32;
                            pf1bit: ColorDepth := 1;
                            pf4bit: ColorDepth := 4;
                            pf8bit: ColorDepth := 8;
                            pf15bit: ColorDepth := 15;
                            pf16bit: ColorDepth := 16;
                            pf24bit: ColorDepth := 24;
                            pf32bit: ColorDepth := 32;
                            pfCustom: ColorDepth := 32;
                        end;
                    finally
                        FreeAndNil(BMPPicture);
                    end;
                end;
                PictureStream.Seek(0, soBeginning);
                //* Add the cover art
                CoverArtData.Name := PwideChar(Description);
                CoverArtData.CoverType := 3; //* ID3v2 cover type (3: front cover)
                CoverArtData.MIMEType := PwideChar(MIMEType);
                CoverArtData.Description := PwideChar(Description);
                CoverArtData.Width := Width;
                CoverArtData.Height := Height;
                CoverArtData.ColorDepth := ColorDepth;
                CoverArtData.NoOfColors := NoOfColors;
                CoverArtData.PictureFormat := CoverArtPictureFormat;
                CoverArtData.Data := PictureStream.Memory;
                CoverArtData.DataSize := PictureStream.Size;
                if TagsLibrary_AddCoverArt(Tags, ttAutomatic, CoverArtData) = - 1 then begin
                    MessageDlg('Error while adding cover art: ' + SaveDialog1.FileName, mtError, [mbCancel], 0);
                end;
            finally
                FreeAndNil(PictureStream);
            end;
        except
            MessageDlg('Error while adding cover art: ' + SaveDialog1.FileName, mtError, [mbCancel], 0);
        end;
    end;
    //* Display added cover art
    ListCoverArts;
end;
Von Delphi zu Delphi sollte das kein Problem sein.
Während das im .NET schon anders aussieht.

gruss
  Mit Zitat antworten Zitat