如何在Delphi 2010中更改TWICImage的像素格式

前端之家收集整理的这篇文章主要介绍了如何在Delphi 2010中更改TWICImage的像素格式前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个TWicImage,IWicBitmap和IWicBitmapSource,可以很好地显示所有支持的图形文件格式,允许旋转,水平翻转,垂直翻转,缩放和剪切。所有这些似乎工作良好,我可以得到WicImages像素格式,但我不知道如何更改或设置TWicImage的像素格式。

我创建了一个对话框,返回WICPixelFormatGUID以用作转换的像素格式。

任何人都可以共享一些代码演示如何使用IWICColorTransform或其他Wincodec方法更改WicImage的像素格式?

法案

它的中间到2011年现在…所以对于那些可能想知道我试过这个,它似乎工作(它使用TcxImage通过Developer Express,但我怀疑TImage将工作)

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,WICBitmapPaletteTypeMedianCut );
      wicImg.ImagingFactory.CreateBitmapFromSourceRect( iConverter,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,GUID_WICPixelFormat16bppGray,WICBitmapDitherTypeSolid,WICBitmapPaletteTypeFixedGray16 );
      wicImg.ImagingFactory.CreateBitmapFromSourceRect( iConverter,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,GUID_WICPixelFormat24bppBGR,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,GUID_WICPixelFormat2bppIndexed,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,GUID_WICPixelFormat32bppGrayFloat,WICBitmapPaletteTypeFixedGray256 );
      wicImg.ImagingFactory.CreateBitmapFromSourceRect( iConverter,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,GUID_WICPixelFormat32bppPBGRA,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,GUID_WICPixelFormat4bppIndexed,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,GUID_WICPixelFormat8bppGray,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,GUID_WICPixelFormat8bppIndexed,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;

解决方法

Bummi和Warren P要求我发布我之前添加的答案。这里是答案:

对于那些可能想知道我试过这个,它似乎工作(它使用TcxImage通过Developer Express,但我怀疑TImage将工作):

procedure TForm1.N16bitBGR1Click( Sender: TObject );
var
  wicImg: TWICImage;
  wicBitmap: IWICBitmap;
  iBmpSource: IWICBitmapSource;
  puiWidth,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;

猜你在找的Delphi相关文章