image

用image控件可以互相转换ico,bmp,jpg三种格式的图片吗

我的代码如下
procedure TForm1.Button1Click(Sender: TObject);
begin
  if OpenDialog1.Execute then
  begin
    try
      image1.Picture.LoadFromFile(OpenDialog1.FileName);
    except
      showmessage('格式不对');
    end;
  end;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  if SaveDialog1.Execute then
  begin
    try
      image1.Picture.SaveToFile(SaveDialog1.FileName);
    except
       showmessage('格式不对');
    end;
  end;
end;

当打开的是bmp图片时,可以保存为bmp,ico.jpg三种格式,且均可以用acdsee查看,
但当是ico图片时,保存为ico格式后可以用acdsee看得到,其余的两种用acdsee
查看时是黑色的!!
当打开的是jpg时报错,
<1>请问如何才能把ico格式的图片真正保存为bmp和jpg格式的图片
<2>在image中怎么才能打开jpg格式的图片,且能把载入的图片分别保存为ico,jpg,bmp
三种格式的
<3>要清空image中的图片,都有哪些方法?
image1.Picture.Assign(nil);对不对
分享到:  
对我有用[0] 丢个板砖[0] 引用 | 举报 | 管理 回复次数:17

Nizvoo
Nizvoo
等级:
#1 得分:0 回复于: 2001-11-30 20:16:55
可以。。。。
可以。。。。
关注CSDN论坛微博 送CSDN积分大礼包对我有用[0] 丢个板砖[0] 引用 | 举报 | 管理

xxy1898
xxy1898
等级:
#2 得分:0 回复于: 2001-11-30 20:23:46
怎么改呀??
传递正能量 赢微软鼠标对我有用[0] 丢个板砖[0] 引用 | 举报 | 管理

thedream
thedream
等级:
#3 得分:0 回复于: 2001-11-30 20:48:53
up
对我有用[0] 丢个板砖[0] 引用 | 举报 | 管理

thedream
thedream
等级:
#4 得分:0 回复于: 2001-11-30 20:49:02
up~~~~
对我有用[0] 丢个板砖[0] 引用 | 举报 | 管理

xxy1898
xxy1898
等级:
#5 得分:0 回复于: 2001-11-30 20:54:08
第二个问题知道了,uses  jpeg就可以了:](可别笑我菜呀)
对我有用[0] 丢个板砖[0] 引用 | 举报 | 管理

zswang
王集鹄
等级:
27
更多勋章
#6 得分:0 回复于: 2001-11-30 20:54:42
function ZsBmpToIco(mBitmap: TBitmap; mIcon: TIcon;
  mSize32: Boolean = True): Boolean; { 返回将位图转换成图标是否成功 }
var
  vIconWidth: Integer;
  vIconHeight: Integer;
  vBitmapMask: TBitmap;
  vBitmapColor: TBitmap;
  vIconInfo: TIconInfo;
begin
  Result := True;
  if mSize32 then begin
    vIconWidth := 32;
    vIconHeight := 32;
  end else begin
    vIconWidth := 16;
    vIconHeight := 16;
  end;

  vBitmapMask := TBitmap.Create;
  vBitmapColor := TBitmap.Create;
  try
    vBitmapMask.Width := vIconWidth;
    vBitmapMask.Height := vIconHeight;
    vBitmapMask.Canvas.Brush.Color := clBlack;
    vBitmapMask.Canvas.Rectangle(0, 0, vIconWidth, vIconHeight);

    vBitmapColor.Width := vIconWidth;
    vBitmapColor.Height := vIconHeight;
    StretchBlt(vBitmapColor.Canvas.Handle, 0, 0, vIconWidth, vIconHeight,
      mBitmap.Canvas.Handle, 0, 0, mBitmap.Width, mBitmap.Height, SRCCOPY);

    vIconInfo.fIcon := True;
    vIconInfo.xHotspot := 0;
    vIconInfo.yHotspot := 0;
    vIconInfo.hbmMask := vBitmapMask.Handle;
    vIconInfo.hbmColor := vBitmapColor.Handle;
    mIcon.Handle := CreateIconIndirect(vIconInfo);
  except
    Result := False;
  end;
  vBitmapMask.Free;
  vBitmapColor.Free;
end; { ZsBmpToIco }
对我有用[0] 丢个板砖[0] 引用 | 举报 | 管理

zswang
王集鹄
等级:
27
更多勋章
#7 得分:0 回复于: 2001-11-30 20:55:49
var
  MyJpeg: TJpegImage;
  bmp: Tbitmap;
begin
  bmp:=tbitmap.Create;
  MyJpeg:= TJpegImage.Create;
  myjpeg.LoadFromFile('c:\windows\desktop\aa.jpg');
  bmp.Assign(myjpeg);
  bmp.SaveToFile('c:\windows\desktop\test.bmp'); // Save the JPEG to Disk
end;
对我有用[0] 丢个板砖[0] 引用 | 举报 | 管理

zswang
王集鹄
等级:
27
更多勋章
#8 得分:0 回复于: 2001-11-30 20:57:48
var
  MyJpeg: TJpegImage;
  bmp: Tbitmap;
begin
  bmp:=tbitmap.Create;
  MyJpeg:= TJpegImage.Create;
  myjpeg.LoadFromFile('c:\windows\desktop\aa.jpg');
  bmp.Assign(myjpeg);
  bmp.SaveToFile('c:\windows\desktop\test.bmp'); // Save the JPEG to Disk
end;
对我有用[0] 丢个板砖[0] 引用 | 举报 | 管理

xxy1898
xxy1898
等级:
#9 得分:0 回复于: 2001-11-30 20:58:17
不好,问题又来了,载入的jpg图片虽然可以保存为bmp和ico格式的
,也可以用acdsee查看,但再次用image载入它们的时候就出错了!!!
对我有用[0] 丢个板砖[0] 引用 | 举报 | 管理

zswang
王集鹄
等级:
27
更多勋章
#10 得分:50 回复于: 2001-11-30 20:59:53
 可以如下,(其他格式也许也可,试一下)记得加入 use jpeg.


var
  MyJpeg: TJpegImage;
  Image1: TImage;
begin
  Image1:= TImage.Create(self);
  MyJpeg:= TJpegImage.Create;
  Image1.Picture.Bitmap.LoadFromFile('c:\windows\desktop\aa.BMP');  // Load the Bitmap from a file
  MyJpeg.Assign(Image1.Picture.Bitmap);  // Assign the BitMap to MyJpeg object
  MyJpeg.CompressionQuality:=StrToInt('75');
  MyJpeg.Compress;
  MyJpeg.SaveToFile('c:\windows\desktop\test.JPG'); // Save the JPEG to Disk
end;
对我有用[0] 丢个板砖[0] 引用 | 举报 | 管理

xxy1898
xxy1898
等级:
#11 得分:0 回复于: 2001-12-01 08:14:35
<3>要清空image中的图片,都有哪些方法?
image1.Picture.Assign(nil);对不对
对我有用[0] 丢个板砖[0] 引用 | 举报 | 管理

xxy1898
xxy1898
等级:
#12 得分:0 回复于: 2001-12-01 09:05:27
我只用一个image和两个button,加SaveDialog1,OpenDialog1,载入的图片有可能是ico,bmp,jpg三种格式,保存时也有三种可能,
那么我的载入代码和保存代码都要考虑这些情况呀!!!
另外载入ico格式的,要保存为bmp格式的怎么写代码转换???
分数不够还可以加呀
对我有用[0] 丢个板砖[0] 引用 | 举报 | 管理

zswang
王集鹄
等级:
27
更多勋章
#13 得分:0 回复于: 2001-12-01 09:44:30
function ZsIcoToBmp(mIcon: TIcon; mBitmap: TBitmap): Boolean;
{ 返回将图标转换成位图是否成功 }
var
  vIconWidth: Integer;
  vIconHeight: Integer;
begin
  Result := True;
  try
    vIconWidth := mIcon.Width;
    vIconHeight := mIcon.Height;
    mBitmap.Width := vIconWidth;
    mBitmap.Height := vIconHeight;
    mBitmap.Canvas.FillRect(Rect(0, 0, vIconWidth, vIconHeight));
    mBitmap.Canvas.Draw(0, 0, mIcon);
  except
    Result := False;
  end;
end; { ZsIcoToBmp }
对我有用[0] 丢个板砖[0] 引用 | 举报 | 管理

zswang
王集鹄
等级:
27
更多勋章
#14 得分:0 回复于: 2001-12-01 10:46:15
//这下你该满足了吧
//pas
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtDlgs, ExtCtrls, jpeg, Spin;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Image1: TImage;
    Image2: TImage;
    Image3: TImage;
    OpenPictureDialog1: TOpenPictureDialog;
    OpenPictureDialog2: TOpenPictureDialog;
    OpenPictureDialog3: TOpenPictureDialog;
    SavePictureDialog1: TSavePictureDialog;
    SavePictureDialog2: TSavePictureDialog;
    SavePictureDialog3: TSavePictureDialog;
    Button2: TButton;
    Button3: TButton;
    Button4: TButton;
    Button5: TButton;
    Button6: TButton;
    Button7: TButton;
    RadioButton1: TRadioButton;
    RadioButton2: TRadioButton;
    RadioButton3: TRadioButton;
    CheckBox1: TCheckBox;
    SpinEdit1: TSpinEdit;
    procedure Button7Click(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button4Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure Button6Click(Sender: TObject);
    procedure Button5Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

function BmpToIco(mBitmap: TBitmap; mIcon: TIcon;
  mSize32: Boolean = True): Boolean;
var
  vIconWidth: Integer;
  vIconHeight: Integer;
  vBitmapMask: TBitmap;
  vBitmapColor: TBitmap;
  vIconInfo: TIconInfo;
begin
  Result := True;
  if mSize32 then begin
    vIconWidth := 32;
    vIconHeight := 32;
  end else begin
    vIconWidth := 16;
    vIconHeight := 16;
  end;

  vBitmapMask := TBitmap.Create;
  vBitmapColor := TBitmap.Create;
  try
    vBitmapMask.Width := vIconWidth;
    vBitmapMask.Height := vIconHeight;
    vBitmapMask.Canvas.Brush.Color := clBlack;
    vBitmapMask.Canvas.Rectangle(0, 0, vIconWidth, vIconHeight);

    vBitmapColor.Width := vIconWidth;
    vBitmapColor.Height := vIconHeight;
    StretchBlt(vBitmapColor.Canvas.Handle, 0, 0, vIconWidth, vIconHeight,
      mBitmap.Canvas.Handle, 0, 0, mBitmap.Width, mBitmap.Height, SRCCOPY);

    vIconInfo.fIcon := True;
    vIconInfo.xHotspot := 0;
    vIconInfo.yHotspot := 0;
    vIconInfo.hbmMask := vBitmapMask.Handle;
    vIconInfo.hbmColor := vBitmapColor.Handle;
    mIcon.Handle := CreateIconIndirect(vIconInfo);
  except
    Result := False;
  end;
  vBitmapMask.Free;
  vBitmapColor.Free;
end; { BmpToIco }

function IcoToBmp(mIcon: TIcon; mBitmap: TBitmap): Boolean;
var
  vIconWidth: Integer;
  vIconHeight: Integer;
begin
  Result := True;
  try
    vIconWidth := mIcon.Width;
    vIconHeight := mIcon.Height;
    mBitmap.Width := vIconWidth;
    mBitmap.Height := vIconHeight;
    mBitmap.Canvas.FillRect(Rect(0, 0, vIconWidth, vIconHeight));
    mBitmap.Canvas.Draw(0, 0, mIcon);
  except
    Result := False;
  end;
end; { IcoToBmp }

function JpegToBmp(mJPEGImage: TJPEGImage; mBitmap: TBitmap): Boolean;
begin
  Result := True;
  try
    mBitmap.Assign(mJPEGImage);
  except
    Result := False;
  end;
end; { JpegToBmp }

function BmpToJpeg(mBitmap: TBitmap; mJPEGImage: TJPEGImage;
  mCompressionQuality: Integer = 75): Boolean;
begin
  Result := True;
  try
    mJPEGImage.Assign(mBitmap);
    mJPEGImage.CompressionQuality := mCompressionQuality;
    mJPEGImage.Compress;
  except
    Result := False;
  end;
end; { BmpToJpeg }

procedure TForm1.Button7Click(Sender: TObject);
var
  vJPEGImage: TJPEGImage;
begin
  if RadioButton1.Checked then begin
    vJPEGImage := TJPEGImage.Create;
    try
      Image2.Picture.Icon.Assign(nil);
      TJPEGImage(Image3.Picture).Assign(nil);
      BmpToIco(Image1.Picture.Bitmap, Image2.Picture.Icon, CheckBox1.Checked);
      BmpToJpeg(Image1.Picture.Bitmap, vJPEGImage, SpinEdit1.Value);
      TJPEGImage(Image3.Picture).Assign(vJPEGImage);
    finally
      vJPEGImage.Free;
    end;
  end else if RadioButton2.Checked then begin
    vJPEGImage := TJPEGImage.Create;
    try
      Image1.Picture.Bitmap.Assign(nil);
      TJPEGImage(Image3.Picture).Assign(nil);
      IcoToBmp(Image2.Picture.Icon, Image1.Picture.Bitmap);
      BmpToJpeg(Image1.Picture.Bitmap, vJPEGImage, SpinEdit1.Value);
      TJPEGImage(Image3.Picture).Assign(vJPEGImage);
    finally
      vJPEGImage.Free;
    end;
  end else begin
    vJPEGImage := TJPEGImage.Create;
    try
      vJPEGImage.Assign(TJPEGImage(Image3.Picture));
      Image1.Picture.Bitmap.Assign(nil);
      Image2.Picture.Icon.Assign(nil);
      JpegToBmp(vJPEGImage, Image1.Picture.Bitmap);
      BmpToIco(Image1.Picture.Bitmap, Image2.Picture.Icon, CheckBox1.Checked);
    finally
      vJPEGImage.Free;
    end;
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  if OpenPictureDialog1.Execute then
    Image1.Picture.Bitmap.LoadFromFile(OpenPictureDialog1.FileName);
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  if SavePictureDialog1.Execute then
    Image1.Picture.Bitmap.SaveToFile(SavePictureDialog1.FileName);
end;

procedure TForm1.Button4Click(Sender: TObject);
begin
  if OpenPictureDialog2.Execute then
    Image2.Picture.Icon.LoadFromFile(OpenPictureDialog2.FileName);
end;

procedure TForm1.Button3Click(Sender: TObject);
begin
  if SavePictureDialog2.Execute then
    Image2.Picture.Icon.SaveToFile(SavePictureDialog2.FileName);
end;

procedure TForm1.Button6Click(Sender: TObject);
begin
  if OpenPictureDialog3.Execute then
    Image3.Picture.LoadFromFile(OpenPictureDialog3.FileName);
end;

procedure TForm1.Button5Click(Sender: TObject);
begin
  if SavePictureDialog3.Execute then
    Image3.Picture.SaveToFile(SavePictureDialog3.FileName);
end;

end.

//dfm
object Form1: TForm1
  Left = 192
  Top = 103
  Width = 544
  Height = 375
  Caption = 'Form1'
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  OldCreateOrder = False
  PixelsPerInch = 96
  TextHeight = 13
  object Image1: TImage
    Left = 24
    Top = 0
    Width = 105
    Height = 105
  end
  object Image2: TImage
    Left = 136
    Top = 0
    Width = 105
    Height = 105
  end
  object Image3: TImage
    Left = 248
    Top = 0
    Width = 105
    Height = 105
  end
  object Button1: TButton
    Left = 24
    Top = 112
    Width = 41
    Height = 25
    Caption = 'Load'
    TabOrder = 0
    OnClick = Button1Click
  end
  object Button2: TButton
    Left = 80
    Top = 112
    Width = 41
    Height = 25
    Caption = 'Save'
    TabOrder = 1
    OnClick = Button2Click
  end
  object Button3: TButton
    Left = 192
    Top = 112
    Width = 41
    Height = 25
    Caption = 'Save'
    TabOrder = 2
    OnClick = Button3Click
  end
  object Button4: TButton
    Left = 136
    Top = 112
    Width = 41
    Height = 25
    Caption = 'Load'
    TabOrder = 3
    OnClick = Button4Click
  end
  object Button5: TButton
    Left = 312
    Top = 112
    Width = 41
    Height = 25
    Caption = 'Save'
    TabOrder = 4
    OnClick = Button5Click
  end
  object Button6: TButton
    Left = 256
    Top = 112
    Width = 41
    Height = 25
    Caption = 'Load'
    TabOrder = 5
    OnClick = Button6Click
  end
  object Button7: TButton
    Left = 32
    Top = 232
    Width = 75
    Height = 25
    Caption = 'Change'
    TabOrder = 6
    OnClick = Button7Click
  end
  object RadioButton1: TRadioButton
    Left = 32
    Top = 152
    Width = 97
    Height = 17
    Caption = 'RadioButton1'
    Checked = True
    TabOrder = 7
    TabStop = True
  end
  object RadioButton2: TRadioButton
    Left = 144
    Top = 152
    Width = 89
    Height = 17
    Caption = 'RadioButton2'
    TabOrder = 8
  end
  object RadioButton3: TRadioButton
    Left = 264
    Top = 152
    Width = 89
    Height = 17
    Caption = 'RadioButton3'
    TabOrder = 9
  end
  object CheckBox1: TCheckBox
    Left = 144
    Top = 176
    Width = 49
    Height = 17
    Caption = '32*32'
    TabOrder = 10
  end
  object SpinEdit1: TSpinEdit
    Left = 264
    Top = 176
    Width = 57
    Height = 22
    MaxValue = 100
    MinValue = 1
    TabOrder = 11
    Value = 0
  end
  object OpenPictureDialog1: TOpenPictureDialog
    Filter = 'Bitmaps (*.bmp)|*.bmp'
    Left = 32
    Top = 24
  end
  object OpenPictureDialog2: TOpenPictureDialog
    Filter = 'Icons (*.ico)|*.ico'
    Left = 144
    Top = 24
  end
  object OpenPictureDialog3: TOpenPictureDialog
    Filter = 'JPEG Image File (*.jpg)|*.jpg|JPEG Image File (*.jpeg)|*.jpeg'
    Left = 264
    Top = 24
  end
  object SavePictureDialog1: TSavePictureDialog
    Filter = 'Bitmaps (*.bmp)|*.bmp'
    Left = 64
    Top = 24
  end
  object SavePictureDialog2: TSavePictureDialog
    Filter = 'Icons (*.ico)|*.ico'
    Left = 176
    Top = 24
  end
  object SavePictureDialog3: TSavePictureDialog
    Filter = 'JPEG Image File (*.jpg)|*.jpg|JPEG Image File (*.jpeg)|*.jpeg'
    Left = 296
    Top = 24
  end
end
对我有用[0] 丢个板砖[0] 引用 | 举报 | 管理

zswang
王集鹄
等级:
27
更多勋章
#15 得分:0 回复于: 2001-12-01 14:26:30
辛苦了半天
分可以不要
但要个回话
对我有用[0] 丢个板砖[0] 引用 | 举报 | 管理

xxy1898
xxy1898
等级:
#16 得分:0 回复于: 2001-12-01 19:12:10
to zswang(伴水)(伤心中):
我对你真是肃然起敬了呀!!!
实在是太麻烦了,谢谢~~~~~~
View Code

 

转载于:https://www.cnblogs.com/blogpro/p/11453865.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值