26

I have a TWicImage, IWicBitmap and a IWicBitmapSource that works nicely to display all the supported graphic fileformats, allows Rotation, Flip Horizontal, Flip Vertical, Scaleing and Clipping. All of these seem to work well and I can get the WicImages pixelformat, but i can not figure out how to change or set a TWicImage's pixelformat.

I created a dialog to return WICPixelFormatGUID to be used as the pixelformat for the transformation.

Can anyone share some code that demonstrates how to change the pixelformat of a WicImage with IWICColorTransform or other Wincodec method?

Bill

Its midway through 2011 now... so for those that may want to know I tried this and it seems to work( it uses TcxImage by Developer Express, but I suspect TImage will work as well):

procedure TForm1.N16bitBGR1Click( Sender: TObject );
var
  wicImg: TWICImage;
  wicBitmap: IWICBitmap;
  iBmpSource: IWICBitmapSource;
  puiWidth, puiHeight: UINT;
  iConverter: IWICFormatConverter;
begin
  if cxImage1.Picture.Graphic is TWICImage then
  begin
    Screen.Cursor := crHourGlass;
    try
      wicImg := TWICImage( cxImage1.Picture.Graphic );
      wicImg.ImagingFactory.CreateFormatConverter( iConverter );
      iBmpSource := wicImg.Handle as IWICBitmapSource;
      iBmpSource.GetSize( puiWidth, puiHeight );
      iConverter.Initialize( iBmpSource, GUID_WICPixelFormat16bppBGR555, WICBitmapDitherTypeNone, nil, 0,
        WICBitmapPaletteTypeMedianCut );
      wicImg.ImagingFactory.CreateBitmapFromSourceRect( iConverter, 0, 0, puiWidth, puiHeight, wicBitmap );
      if Assigned( wicBitmap ) then
        wicImg.Handle := wicBitmap;
      cxImage1.Repaint;
      cxImage1.Update;
      cxImage1.Invalidate;
      dxStatusBar1.Panels[ 0 ].Text := ExtractFileDir( AFilename );
      dxStatusBar1.Panels[ 1 ].Text := ExtractFileName( AFilename );
      dxStatusBar1.Panels[ 2 ].Text := 'Width: ' + IntToStr( WICImageWidth( cxImage1 ) );
      dxStatusBar1.Panels[ 3 ].Text := 'Height: ' + IntToStr( WICImageHeight( cxImage1 ) );
      dxStatusBar1.Panels[ 4 ].Text := 'Pixel Format: ' + WICGetPixelFormat( cxImage1 );
    finally
      Screen.Cursor := crDefault;
    end;
  end;
end;

procedure TForm1.N16bitGray1Click( Sender: TObject );
var
  wicImg: TWICImage;
  wicBitmap: IWICBitmap;
  iBmpSource: IWICBitmapSource;
  puiWidth, puiHeight: UINT;
  iConverter: IWICFormatConverter;
begin
  if cxImage1.Picture.Graphic is TWICImage then
  begin
    Screen.Cursor := crHourGlass;
    try
      wicImg := TWICImage( cxImage1.Picture.Graphic );
      wicImg.ImagingFactory.CreateFormatConverter( iConverter );
      iBmpSource := wicImg.Handle as IWICBitmapSource;
      iBmpSource.GetSize( puiWidth, puiHeight );
      iConverter.Initialize( iBmpSource, GUID_WICPixelFormat16bppGray, WICBitmapDitherTypeSolid, nil, 0,
        WICBitmapPaletteTypeFixedGray16 );
      wicImg.ImagingFactory.CreateBitmapFromSourceRect( iConverter, 0, 0, puiWidth, puiHeight, wicBitmap );
      if Assigned( wicBitmap ) then
        wicImg.Handle := wicBitmap;
      cxImage1.Repaint;
      cxImage1.Update;
      cxImage1.Invalidate;
      dxStatusBar1.Panels[ 0 ].Text := ExtractFileDir( AFilename );
      dxStatusBar1.Panels[ 1 ].Text := ExtractFileName( AFilename );
      dxStatusBar1.Panels[ 2 ].Text := 'Width: ' + IntToStr( WICImageWidth( cxImage1 ) );
      dxStatusBar1.Panels[ 3 ].Text := 'Height: ' + IntToStr( WICImageHeight( cxImage1 ) );
      dxStatusBar1.Panels[ 4 ].Text := 'Pixel Format: ' + WICGetPixelFormat( cxImage1 );
    finally
      Screen.Cursor := crDefault;
    end;
  end;
end;

procedure TForm1.N24bitGBB1Click( Sender: TObject );
var
  wicImg: TWICImage;
  wicBitmap: IWICBitmap;
  iBmpSource: IWICBitmapSource;
  puiWidth, puiHeight: UINT;
  iConverter: IWICFormatConverter;
begin
  if cxImage1.Picture.Graphic is TWICImage then
  begin
    Screen.Cursor := crHourGlass;
    try
      wicImg := TWICImage( cxImage1.Picture.Graphic );
      wicImg.ImagingFactory.CreateFormatConverter( iConverter );
      iBmpSource := wicImg.Handle as IWICBitmapSource;
      iBmpSource.GetSize( puiWidth, puiHeight );
      iConverter.Initialize( iBmpSource, GUID_WICPixelFormat24bppBGR, WICBitmapDitherTypeNone, nil, 0,
        WICBitmapPaletteTypeMedianCut );
      wicImg.ImagingFactory.CreateBitmapFromSourceRect( iConverter, 0, 0, puiWidth, puiHeight, wicBitmap );
      if Assigned( wicBitmap ) then
        wicImg.Handle := wicBitmap;
      cxImage1.Repaint;
      cxImage1.Update;
      cxImage1.Invalidate;
      dxStatusBar1.Panels[ 0 ].Text := ExtractFileDir( AFilename );
      dxStatusBar1.Panels[ 1 ].Text := ExtractFileName( AFilename );
      dxStatusBar1.Panels[ 2 ].Text := 'Width: ' + IntToStr( WICImageWidth( cxImage1 ) );
      dxStatusBar1.Panels[ 3 ].Text := 'Height: ' + IntToStr( WICImageHeight( cxImage1 ) );
      dxStatusBar1.Panels[ 4 ].Text := 'Pixel Format: ' + WICGetPixelFormat( cxImage1 );
    finally
      Screen.Cursor := crDefault;
    end;
  end;
end;

procedure TForm1.N2bitIndexed1Click( Sender: TObject );
var
  wicImg: TWICImage;
  wicBitmap: IWICBitmap;
  iBmpSource: IWICBitmapSource;
  puiWidth, puiHeight: UINT;
  iConverter: IWICFormatConverter;
begin
  if cxImage1.Picture.Graphic is TWICImage then
  begin
    Screen.Cursor := crHourGlass;
    try
      wicImg := TWICImage( cxImage1.Picture.Graphic );
      wicImg.ImagingFactory.CreateFormatConverter( iConverter );
      iBmpSource := wicImg.Handle as IWICBitmapSource;
      iBmpSource.GetSize( puiWidth, puiHeight );
      iConverter.Initialize( iBmpSource, GUID_WICPixelFormat2bppIndexed, WICBitmapDitherTypeNone, nil, 0,
        WICBitmapPaletteTypeMedianCut );
      wicImg.ImagingFactory.CreateBitmapFromSourceRect( iConverter, 0, 0, puiWidth, puiHeight, wicBitmap );
      if Assigned( wicBitmap ) then
        wicImg.Handle := wicBitmap;
      cxImage1.Repaint;
      cxImage1.Update;
      cxImage1.Invalidate;
      dxStatusBar1.Panels[ 0 ].Text := ExtractFileDir( AFilename );
      dxStatusBar1.Panels[ 1 ].Text := ExtractFileName( AFilename );
      dxStatusBar1.Panels[ 2 ].Text := 'Width: ' + IntToStr( WICImageWidth( cxImage1 ) );
      dxStatusBar1.Panels[ 3 ].Text := 'Height: ' + IntToStr( WICImageHeight( cxImage1 ) );
      dxStatusBar1.Panels[ 4 ].Text := 'Pixel Format: ' + WICGetPixelFormat( cxImage1 );
    finally
      Screen.Cursor := crDefault;
    end;
  end;
end;

procedure TForm1.N32bitGray1Click( Sender: TObject );
var
  wicImg: TWICImage;
  wicBitmap: IWICBitmap;
  iBmpSource: IWICBitmapSource;
  puiWidth, puiHeight: UINT;
  iConverter: IWICFormatConverter;
begin
  if cxImage1.Picture.Graphic is TWICImage then
  begin
    Screen.Cursor := crHourGlass;
    try
      wicImg := TWICImage( cxImage1.Picture.Graphic );
      wicImg.ImagingFactory.CreateFormatConverter( iConverter );
      iBmpSource := wicImg.Handle as IWICBitmapSource;
      iBmpSource.GetSize( puiWidth, puiHeight );
      iConverter.Initialize( iBmpSource, GUID_WICPixelFormat32bppGrayFloat, WICBitmapDitherTypeSolid, nil, 0,
        WICBitmapPaletteTypeFixedGray256 );
      wicImg.ImagingFactory.CreateBitmapFromSourceRect( iConverter, 0, 0, puiWidth, puiHeight, wicBitmap );
      if Assigned( wicBitmap ) then
        wicImg.Handle := wicBitmap;
      cxImage1.Repaint;
      cxImage1.Update;
      cxImage1.Invalidate;
      dxStatusBar1.Panels[ 0 ].Text := ExtractFileDir( AFilename );
      dxStatusBar1.Panels[ 1 ].Text := ExtractFileName( AFilename );
      dxStatusBar1.Panels[ 2 ].Text := 'Width: ' + IntToStr( WICImageWidth( cxImage1 ) );
      dxStatusBar1.Panels[ 3 ].Text := 'Height: ' + IntToStr( WICImageHeight( cxImage1 ) );
      dxStatusBar1.Panels[ 4 ].Text := 'Pixel Format: ' + WICGetPixelFormat( cxImage1 );
    finally
      Screen.Cursor := crDefault;
    end;
  end;
end;

procedure TForm1.N32bitGRBA1Click( Sender: TObject );
var
  wicImg: TWICImage;
  wicBitmap: IWICBitmap;
  iBmpSource: IWICBitmapSource;
  puiWidth, puiHeight: UINT;
  iConverter: IWICFormatConverter;
begin
  if cxImage1.Picture.Graphic is TWICImage then
  begin
    Screen.Cursor := crHourGlass;
    try
      wicImg := TWICImage( cxImage1.Picture.Graphic );
      wicImg.ImagingFactory.CreateFormatConverter( iConverter );
      iBmpSource := wicImg.Handle as IWICBitmapSource;
      iBmpSource.GetSize( puiWidth, puiHeight );
      iConverter.Initialize( iBmpSource, GUID_WICPixelFormat32bppPBGRA, WICBitmapDitherTypeNone, nil, 0,
        WICBitmapPaletteTypeMedianCut );
      wicImg.ImagingFactory.CreateBitmapFromSourceRect( iConverter, 0, 0, puiWidth, puiHeight, wicBitmap );
      if Assigned( wicBitmap ) then
        wicImg.Handle := wicBitmap;
      cxImage1.Repaint;
      cxImage1.Update;
      cxImage1.Invalidate;
      dxStatusBar1.Panels[ 0 ].Text := ExtractFileDir( AFilename );
      dxStatusBar1.Panels[ 1 ].Text := ExtractFileName( AFilename );
      dxStatusBar1.Panels[ 2 ].Text := 'Width: ' + IntToStr( WICImageWidth( cxImage1 ) );
      dxStatusBar1.Panels[ 3 ].Text := 'Height: ' + IntToStr( WICImageHeight( cxImage1 ) );
      dxStatusBar1.Panels[ 4 ].Text := 'Pixel Format: ' + WICGetPixelFormat( cxImage1 );
    finally
      Screen.Cursor := crDefault;
    end;
  end;
end;

procedure TForm1.N4bitIndexed1Click( Sender: TObject );
var
  wicImg: TWICImage;
  wicBitmap: IWICBitmap;
  iBmpSource: IWICBitmapSource;
  puiWidth, puiHeight: UINT;
  iConverter: IWICFormatConverter;
begin
  if cxImage1.Picture.Graphic is TWICImage then
  begin
    Screen.Cursor := crHourGlass;
    try
      wicImg := TWICImage( cxImage1.Picture.Graphic );
      wicImg.ImagingFactory.CreateFormatConverter( iConverter );
      iBmpSource := wicImg.Handle as IWICBitmapSource;
      iBmpSource.GetSize( puiWidth, puiHeight );
      iConverter.Initialize( iBmpSource, GUID_WICPixelFormat4bppIndexed, WICBitmapDitherTypeNone, nil, 0,
        WICBitmapPaletteTypeMedianCut );
      wicImg.ImagingFactory.CreateBitmapFromSourceRect( iConverter, 0, 0, puiWidth, puiHeight, wicBitmap );
      if Assigned( wicBitmap ) then
        wicImg.Handle := wicBitmap;
      cxImage1.Repaint;
      cxImage1.Update;
      cxImage1.Invalidate;
      dxStatusBar1.Panels[ 0 ].Text := ExtractFileDir( AFilename );
      dxStatusBar1.Panels[ 1 ].Text := ExtractFileName( AFilename );
      dxStatusBar1.Panels[ 2 ].Text := 'Width: ' + IntToStr( WICImageWidth( cxImage1 ) );
      dxStatusBar1.Panels[ 3 ].Text := 'Height: ' + IntToStr( WICImageHeight( cxImage1 ) );
      dxStatusBar1.Panels[ 4 ].Text := 'Pixel Format: ' + WICGetPixelFormat( cxImage1 );
    finally
      Screen.Cursor := crDefault;
    end;
  end;
end;

procedure TForm1.N8bitGray1Click( Sender: TObject );
var
  wicImg: TWICImage;
  wicBitmap: IWICBitmap;
  iBmpSource: IWICBitmapSource;
  puiWidth, puiHeight: UINT;
  iConverter: IWICFormatConverter;
begin
  if cxImage1.Picture.Graphic is TWICImage then
  begin
    Screen.Cursor := crHourGlass;
    try
      wicImg := TWICImage( cxImage1.Picture.Graphic );
      wicImg.ImagingFactory.CreateFormatConverter( iConverter );
      iBmpSource := wicImg.Handle as IWICBitmapSource;
      iBmpSource.GetSize( puiWidth, puiHeight );
      iConverter.Initialize( iBmpSource, GUID_WICPixelFormat8bppGray, WICBitmapDitherTypeSolid, nil, 0,
        WICBitmapPaletteTypeMedianCut );
      wicImg.ImagingFactory.CreateBitmapFromSourceRect( iConverter, 0, 0, puiWidth, puiHeight, wicBitmap );
      if Assigned( wicBitmap ) then
        wicImg.Handle := wicBitmap;
      cxImage1.Repaint;
      cxImage1.Update;
      cxImage1.Invalidate;
      dxStatusBar1.Panels[ 0 ].Text := ExtractFileDir( AFilename );
      dxStatusBar1.Panels[ 1 ].Text := ExtractFileName( AFilename );
      dxStatusBar1.Panels[ 2 ].Text := 'Width: ' + IntToStr( WICImageWidth( cxImage1 ) );
      dxStatusBar1.Panels[ 3 ].Text := 'Height: ' + IntToStr( WICImageHeight( cxImage1 ) );
      dxStatusBar1.Panels[ 4 ].Text := 'Pixel Format: ' + WICGetPixelFormat( cxImage1 );
    finally
      Screen.Cursor := crDefault;
    end;
  end;
end;

procedure TForm1.N8bitIndexed1Click( Sender: TObject );
var
  wicImg: TWICImage;
  wicBitmap: IWICBitmap;
  iBmpSource: IWICBitmapSource;
  puiWidth, puiHeight: UINT;
  iConverter: IWICFormatConverter;
begin
  if cxImage1.Picture.Graphic is TWICImage then
  begin
    Screen.Cursor := crHourGlass;
    try
      wicImg := TWICImage( cxImage1.Picture.Graphic );
      wicImg.ImagingFactory.CreateFormatConverter( iConverter );
      iBmpSource := wicImg.Handle as IWICBitmapSource;
      iBmpSource.GetSize( puiWidth, puiHeight );
      iConverter.Initialize( iBmpSource, GUID_WICPixelFormat8bppIndexed, WICBitmapDitherTypeNone, nil, 0,
        WICBitmapPaletteTypeFixedGray256 );
      wicImg.ImagingFactory.CreateBitmapFromSourceRect( iConverter, 0, 0, puiWidth, puiHeight, wicBitmap );
      if Assigned( wicBitmap ) then
        wicImg.Handle := wicBitmap;
      cxImage1.Repaint;
      cxImage1.Update;
      cxImage1.Invalidate;
      dxStatusBar1.Panels[ 0 ].Text := ExtractFileDir( AFilename );
      dxStatusBar1.Panels[ 1 ].Text := ExtractFileName( AFilename );
      dxStatusBar1.Panels[ 2 ].Text := 'Width: ' + IntToStr( WICImageWidth( cxImage1 ) );
      dxStatusBar1.Panels[ 3 ].Text := 'Height: ' + IntToStr( WICImageHeight( cxImage1 ) );
      dxStatusBar1.Panels[ 4 ].Text := 'Pixel Format: ' + WICGetPixelFormat( cxImage1 );
    finally
      Screen.Cursor := crDefault;
    end;
  end;
end;
gustavogbc
  • 695
  • 11
  • 33
Bill
  • 2,993
  • 5
  • 37
  • 71
  • 1
    +1, anyway, you can simply use the [`WICConvertBitmapSource`](http://msdn.microsoft.com/en-us/library/windows/desktop/ee719819(v=vs.85).aspx) function without need of format converter for this. – TLama Sep 11 '12 at 15:34
  • 1
    Hi Bill, your answer in the Top of the unanswered Delphi questions. If I read it correctly you solved it and put your answer into your question. Could you place the answer into the answer section and accept it? – bummi Sep 27 '14 at 20:39
  • Its now november and that's not done, I suggest someone do it for him, with attribution. – Warren P Nov 21 '14 at 14:29

1 Answers1

2

Bummi and Warren P asked that I post the answer that I had added sometime ago. Here is the answer:

For those that may want to know I tried this and it seems to work (it uses TcxImage by Developer Express, but I suspect TImage will work as well):

procedure TForm1.N16bitBGR1Click( Sender: TObject );
var
  wicImg: TWICImage;
  wicBitmap: IWICBitmap;
  iBmpSource: IWICBitmapSource;
  puiWidth, puiHeight: UINT;
  iConverter: IWICFormatConverter;
begin
  if cxImage1.Picture.Graphic is TWICImage then
  begin
    Screen.Cursor := crHourGlass;
    try
      wicImg := TWICImage( cxImage1.Picture.Graphic );
      wicImg.ImagingFactory.CreateFormatConverter( iConverter );
      iBmpSource := wicImg.Handle as IWICBitmapSource;
      iBmpSource.GetSize( puiWidth, puiHeight );
      iConverter.Initialize( iBmpSource, GUID_WICPixelFormat16bppBGR555, WICBitmapDitherTypeNone, nil, 0,
        WICBitmapPaletteTypeMedianCut );
      wicImg.ImagingFactory.CreateBitmapFromSourceRect( iConverter, 0, 0, puiWidth, puiHeight, wicBitmap );
      if Assigned( wicBitmap ) then
        wicImg.Handle := wicBitmap;
      cxImage1.Repaint;
      cxImage1.Update;
      cxImage1.Invalidate;
      dxStatusBar1.Panels[ 0 ].Text := ExtractFileDir( AFilename );
      dxStatusBar1.Panels[ 1 ].Text := ExtractFileName( AFilename );
      dxStatusBar1.Panels[ 2 ].Text := 'Width: ' + IntToStr( WICImageWidth( cxImage1 ) );
      dxStatusBar1.Panels[ 3 ].Text := 'Height: ' + IntToStr( WICImageHeight( cxImage1 ) );
      dxStatusBar1.Panels[ 4 ].Text := 'Pixel Format: ' + WICGetPixelFormat( cxImage1 );
    finally
      Screen.Cursor := crDefault;
    end;
  end;
end;

procedure TForm1.N16bitGray1Click( Sender: TObject );
var
  wicImg: TWICImage;
  wicBitmap: IWICBitmap;
  iBmpSource: IWICBitmapSource;
  puiWidth, puiHeight: UINT;
  iConverter: IWICFormatConverter;
begin
  if cxImage1.Picture.Graphic is TWICImage then
  begin
    Screen.Cursor := crHourGlass;
    try
      wicImg := TWICImage( cxImage1.Picture.Graphic );
      wicImg.ImagingFactory.CreateFormatConverter( iConverter );
      iBmpSource := wicImg.Handle as IWICBitmapSource;
      iBmpSource.GetSize( puiWidth, puiHeight );
      iConverter.Initialize( iBmpSource, GUID_WICPixelFormat16bppGray, WICBitmapDitherTypeSolid, nil, 0,
        WICBitmapPaletteTypeFixedGray16 );
      wicImg.ImagingFactory.CreateBitmapFromSourceRect( iConverter, 0, 0, puiWidth, puiHeight, wicBitmap );
      if Assigned( wicBitmap ) then
        wicImg.Handle := wicBitmap;
      cxImage1.Repaint;
      cxImage1.Update;
      cxImage1.Invalidate;
      dxStatusBar1.Panels[ 0 ].Text := ExtractFileDir( AFilename );
      dxStatusBar1.Panels[ 1 ].Text := ExtractFileName( AFilename );
      dxStatusBar1.Panels[ 2 ].Text := 'Width: ' + IntToStr( WICImageWidth( cxImage1 ) );
      dxStatusBar1.Panels[ 3 ].Text := 'Height: ' + IntToStr( WICImageHeight( cxImage1 ) );
      dxStatusBar1.Panels[ 4 ].Text := 'Pixel Format: ' + WICGetPixelFormat( cxImage1 );
    finally
      Screen.Cursor := crDefault;
    end;
  end;
end;

procedure TForm1.N24bitGBB1Click( Sender: TObject );
var
  wicImg: TWICImage;
  wicBitmap: IWICBitmap;
  iBmpSource: IWICBitmapSource;
  puiWidth, puiHeight: UINT;
  iConverter: IWICFormatConverter;
begin
  if cxImage1.Picture.Graphic is TWICImage then
  begin
    Screen.Cursor := crHourGlass;
    try
      wicImg := TWICImage( cxImage1.Picture.Graphic );
      wicImg.ImagingFactory.CreateFormatConverter( iConverter );
      iBmpSource := wicImg.Handle as IWICBitmapSource;
      iBmpSource.GetSize( puiWidth, puiHeight );
      iConverter.Initialize( iBmpSource, GUID_WICPixelFormat24bppBGR, WICBitmapDitherTypeNone, nil, 0,
        WICBitmapPaletteTypeMedianCut );
      wicImg.ImagingFactory.CreateBitmapFromSourceRect( iConverter, 0, 0, puiWidth, puiHeight, wicBitmap );
      if Assigned( wicBitmap ) then
        wicImg.Handle := wicBitmap;
      cxImage1.Repaint;
      cxImage1.Update;
      cxImage1.Invalidate;
      dxStatusBar1.Panels[ 0 ].Text := ExtractFileDir( AFilename );
      dxStatusBar1.Panels[ 1 ].Text := ExtractFileName( AFilename );
      dxStatusBar1.Panels[ 2 ].Text := 'Width: ' + IntToStr( WICImageWidth( cxImage1 ) );
      dxStatusBar1.Panels[ 3 ].Text := 'Height: ' + IntToStr( WICImageHeight( cxImage1 ) );
      dxStatusBar1.Panels[ 4 ].Text := 'Pixel Format: ' + WICGetPixelFormat( cxImage1 );
    finally
      Screen.Cursor := crDefault;
    end;
  end;
end;

procedure TForm1.N2bitIndexed1Click( Sender: TObject );
var
  wicImg: TWICImage;
  wicBitmap: IWICBitmap;
  iBmpSource: IWICBitmapSource;
  puiWidth, puiHeight: UINT;
  iConverter: IWICFormatConverter;
begin
  if cxImage1.Picture.Graphic is TWICImage then
  begin
    Screen.Cursor := crHourGlass;
    try
      wicImg := TWICImage( cxImage1.Picture.Graphic );
      wicImg.ImagingFactory.CreateFormatConverter( iConverter );
      iBmpSource := wicImg.Handle as IWICBitmapSource;
      iBmpSource.GetSize( puiWidth, puiHeight );
      iConverter.Initialize( iBmpSource, GUID_WICPixelFormat2bppIndexed, WICBitmapDitherTypeNone, nil, 0,
        WICBitmapPaletteTypeMedianCut );
      wicImg.ImagingFactory.CreateBitmapFromSourceRect( iConverter, 0, 0, puiWidth, puiHeight, wicBitmap );
      if Assigned( wicBitmap ) then
        wicImg.Handle := wicBitmap;
      cxImage1.Repaint;
      cxImage1.Update;
      cxImage1.Invalidate;
      dxStatusBar1.Panels[ 0 ].Text := ExtractFileDir( AFilename );
      dxStatusBar1.Panels[ 1 ].Text := ExtractFileName( AFilename );
      dxStatusBar1.Panels[ 2 ].Text := 'Width: ' + IntToStr( WICImageWidth( cxImage1 ) );
      dxStatusBar1.Panels[ 3 ].Text := 'Height: ' + IntToStr( WICImageHeight( cxImage1 ) );
      dxStatusBar1.Panels[ 4 ].Text := 'Pixel Format: ' + WICGetPixelFormat( cxImage1 );
    finally
      Screen.Cursor := crDefault;
    end;
  end;
end;

procedure TForm1.N32bitGray1Click( Sender: TObject );
var
  wicImg: TWICImage;
  wicBitmap: IWICBitmap;
  iBmpSource: IWICBitmapSource;
  puiWidth, puiHeight: UINT;
  iConverter: IWICFormatConverter;
begin
  if cxImage1.Picture.Graphic is TWICImage then
  begin
    Screen.Cursor := crHourGlass;
    try
      wicImg := TWICImage( cxImage1.Picture.Graphic );
      wicImg.ImagingFactory.CreateFormatConverter( iConverter );
      iBmpSource := wicImg.Handle as IWICBitmapSource;
      iBmpSource.GetSize( puiWidth, puiHeight );
      iConverter.Initialize( iBmpSource, GUID_WICPixelFormat32bppGrayFloat, WICBitmapDitherTypeSolid, nil, 0,
        WICBitmapPaletteTypeFixedGray256 );
      wicImg.ImagingFactory.CreateBitmapFromSourceRect( iConverter, 0, 0, puiWidth, puiHeight, wicBitmap );
      if Assigned( wicBitmap ) then
        wicImg.Handle := wicBitmap;
      cxImage1.Repaint;
      cxImage1.Update;
      cxImage1.Invalidate;
      dxStatusBar1.Panels[ 0 ].Text := ExtractFileDir( AFilename );
      dxStatusBar1.Panels[ 1 ].Text := ExtractFileName( AFilename );
      dxStatusBar1.Panels[ 2 ].Text := 'Width: ' + IntToStr( WICImageWidth( cxImage1 ) );
      dxStatusBar1.Panels[ 3 ].Text := 'Height: ' + IntToStr( WICImageHeight( cxImage1 ) );
      dxStatusBar1.Panels[ 4 ].Text := 'Pixel Format: ' + WICGetPixelFormat( cxImage1 );
    finally
      Screen.Cursor := crDefault;
    end;
  end;
end;

procedure TForm1.N32bitGRBA1Click( Sender: TObject );
var
  wicImg: TWICImage;
  wicBitmap: IWICBitmap;
  iBmpSource: IWICBitmapSource;
  puiWidth, puiHeight: UINT;
  iConverter: IWICFormatConverter;
begin
  if cxImage1.Picture.Graphic is TWICImage then
  begin
    Screen.Cursor := crHourGlass;
    try
      wicImg := TWICImage( cxImage1.Picture.Graphic );
      wicImg.ImagingFactory.CreateFormatConverter( iConverter );
      iBmpSource := wicImg.Handle as IWICBitmapSource;
      iBmpSource.GetSize( puiWidth, puiHeight );
      iConverter.Initialize( iBmpSource, GUID_WICPixelFormat32bppPBGRA, WICBitmapDitherTypeNone, nil, 0,
        WICBitmapPaletteTypeMedianCut );
      wicImg.ImagingFactory.CreateBitmapFromSourceRect( iConverter, 0, 0, puiWidth, puiHeight, wicBitmap );
      if Assigned( wicBitmap ) then
        wicImg.Handle := wicBitmap;
      cxImage1.Repaint;
      cxImage1.Update;
      cxImage1.Invalidate;
      dxStatusBar1.Panels[ 0 ].Text := ExtractFileDir( AFilename );
      dxStatusBar1.Panels[ 1 ].Text := ExtractFileName( AFilename );
      dxStatusBar1.Panels[ 2 ].Text := 'Width: ' + IntToStr( WICImageWidth( cxImage1 ) );
      dxStatusBar1.Panels[ 3 ].Text := 'Height: ' + IntToStr( WICImageHeight( cxImage1 ) );
      dxStatusBar1.Panels[ 4 ].Text := 'Pixel Format: ' + WICGetPixelFormat( cxImage1 );
    finally
      Screen.Cursor := crDefault;
    end;
  end;
end;

procedure TForm1.N4bitIndexed1Click( Sender: TObject );
var
  wicImg: TWICImage;
  wicBitmap: IWICBitmap;
  iBmpSource: IWICBitmapSource;
  puiWidth, puiHeight: UINT;
  iConverter: IWICFormatConverter;
begin
  if cxImage1.Picture.Graphic is TWICImage then
  begin
    Screen.Cursor := crHourGlass;
    try
      wicImg := TWICImage( cxImage1.Picture.Graphic );
      wicImg.ImagingFactory.CreateFormatConverter( iConverter );
      iBmpSource := wicImg.Handle as IWICBitmapSource;
      iBmpSource.GetSize( puiWidth, puiHeight );
      iConverter.Initialize( iBmpSource, GUID_WICPixelFormat4bppIndexed, WICBitmapDitherTypeNone, nil, 0,
        WICBitmapPaletteTypeMedianCut );
      wicImg.ImagingFactory.CreateBitmapFromSourceRect( iConverter, 0, 0, puiWidth, puiHeight, wicBitmap );
      if Assigned( wicBitmap ) then
        wicImg.Handle := wicBitmap;
      cxImage1.Repaint;
      cxImage1.Update;
      cxImage1.Invalidate;
      dxStatusBar1.Panels[ 0 ].Text := ExtractFileDir( AFilename );
      dxStatusBar1.Panels[ 1 ].Text := ExtractFileName( AFilename );
      dxStatusBar1.Panels[ 2 ].Text := 'Width: ' + IntToStr( WICImageWidth( cxImage1 ) );
      dxStatusBar1.Panels[ 3 ].Text := 'Height: ' + IntToStr( WICImageHeight( cxImage1 ) );
      dxStatusBar1.Panels[ 4 ].Text := 'Pixel Format: ' + WICGetPixelFormat( cxImage1 );
    finally
      Screen.Cursor := crDefault;
    end;
  end;
end;

procedure TForm1.N8bitGray1Click( Sender: TObject );
var
  wicImg: TWICImage;
  wicBitmap: IWICBitmap;
  iBmpSource: IWICBitmapSource;
  puiWidth, puiHeight: UINT;
  iConverter: IWICFormatConverter;
begin
  if cxImage1.Picture.Graphic is TWICImage then
  begin
    Screen.Cursor := crHourGlass;
    try
      wicImg := TWICImage( cxImage1.Picture.Graphic );
      wicImg.ImagingFactory.CreateFormatConverter( iConverter );
      iBmpSource := wicImg.Handle as IWICBitmapSource;
      iBmpSource.GetSize( puiWidth, puiHeight );
      iConverter.Initialize( iBmpSource, GUID_WICPixelFormat8bppGray, WICBitmapDitherTypeSolid, nil, 0,
        WICBitmapPaletteTypeMedianCut );
      wicImg.ImagingFactory.CreateBitmapFromSourceRect( iConverter, 0, 0, puiWidth, puiHeight, wicBitmap );
      if Assigned( wicBitmap ) then
        wicImg.Handle := wicBitmap;
      cxImage1.Repaint;
      cxImage1.Update;
      cxImage1.Invalidate;
      dxStatusBar1.Panels[ 0 ].Text := ExtractFileDir( AFilename );
      dxStatusBar1.Panels[ 1 ].Text := ExtractFileName( AFilename );
      dxStatusBar1.Panels[ 2 ].Text := 'Width: ' + IntToStr( WICImageWidth( cxImage1 ) );
      dxStatusBar1.Panels[ 3 ].Text := 'Height: ' + IntToStr( WICImageHeight( cxImage1 ) );
      dxStatusBar1.Panels[ 4 ].Text := 'Pixel Format: ' + WICGetPixelFormat( cxImage1 );
    finally
      Screen.Cursor := crDefault;
    end;
  end;
end;

procedure TForm1.N8bitIndexed1Click( Sender: TObject );
var
  wicImg: TWICImage;
  wicBitmap: IWICBitmap;
  iBmpSource: IWICBitmapSource;
  puiWidth, puiHeight: UINT;
  iConverter: IWICFormatConverter;
begin
  if cxImage1.Picture.Graphic is TWICImage then
  begin
    Screen.Cursor := crHourGlass;
    try
      wicImg := TWICImage( cxImage1.Picture.Graphic );
      wicImg.ImagingFactory.CreateFormatConverter( iConverter );
      iBmpSource := wicImg.Handle as IWICBitmapSource;
      iBmpSource.GetSize( puiWidth, puiHeight );
      iConverter.Initialize( iBmpSource, GUID_WICPixelFormat8bppIndexed, WICBitmapDitherTypeNone, nil, 0,
        WICBitmapPaletteTypeFixedGray256 );
      wicImg.ImagingFactory.CreateBitmapFromSourceRect( iConverter, 0, 0, puiWidth, puiHeight, wicBitmap );
      if Assigned( wicBitmap ) then
        wicImg.Handle := wicBitmap;
      cxImage1.Repaint;
      cxImage1.Update;
      cxImage1.Invalidate;
      dxStatusBar1.Panels[ 0 ].Text := ExtractFileDir( AFilename );
      dxStatusBar1.Panels[ 1 ].Text := ExtractFileName( AFilename );
      dxStatusBar1.Panels[ 2 ].Text := 'Width: ' + IntToStr( WICImageWidth( cxImage1 ) );
      dxStatusBar1.Panels[ 3 ].Text := 'Height: ' + IntToStr( WICImageHeight( cxImage1 ) );
      dxStatusBar1.Panels[ 4 ].Text := 'Pixel Format: ' + WICGetPixelFormat( cxImage1 );
    finally
      Screen.Cursor := crDefault;
    end;
  end;
end;
Bill
  • 2,993
  • 5
  • 37
  • 71