rcp mysql_RCP编程技巧:如何显示Mysql数据库中存储的图片?

我在程序开发中,经常要将图片文件存入Mysql数据库的longblob字段中,存入数据库和以前在jsp中没什么区别,那么,如何将存入数据库的图片在SWT中显示呢?

首先,我写了一个方法,将inputstream取出,

public InputStream getImage(String id) {

DataConfig config = new DataConfig();

InputStream stream = null;

try {

config.openConnection();

Connection conn = config.getConnection();

PreparedStatement pStmt = conn

.prepareStatement("select bugimage from bugs where id=?");

pStmt.setString(1, id);

ResultSet rs = pStmt.executeQuery();

if (rs != null) {

while (rs.next()) {

stream = rs.getBinaryStream(1);

}

rs.close();

}

pStmt.close();

} catch (SQLException e) {

e.printStackTrace();

}

return stream;

}

然后,在程序中使用SWT的Image类和Canvas类将图片显示出来,代码如下:

//其中Canvas显示滚动条的代码来自eclipse网站

Composite composite = new Composite(parent, SWT.NONE);

GridLayout gridLayout = new GridLayout(1, false);

gridLayout.marginWidth = 10;

gridLayout.marginHeight = 10;

gridLayout.horizontalSpacing = 10;

gridLayout.verticalSpacing = 10;

composite.setLayout(gridLayout);

GridData gridData = new GridData(GridData.FILL, GridData.CENTER, false,

false);

composite.setLayoutData(gridData);

image = new Image(Display.getDefault(), getImage("1"));

if (image == null) {

int width = 150, height = 200;

image = new Image(FireiceUtil.getDisplay(), width, height);

GC gc = new GC(image);

gc.fillRectangle(0, 0, width, height);

gc.drawLine(0, 0, width, height);

gc.drawLine(0, height, width, 0);

gc.drawText("No Image", 10, 10);

gc.dispose();

}

canvas = new Canvas(composite, SWT.NO_BACKGROUND | SWT.NO_REDRAW_RESIZE | SWT.V_SCROLL | SWT.H_SCROLL);

GridData canvasGridData = new GridData();

canvasGridData.heightHint = 400;

canvasGridData.widthHint = 500;

canvas.setLayoutData(canvasGridData);

final Point origin = new Point(0, 0);

final ScrollBar hBar = canvas.getHorizontalBar();

hBar.addListener(SWT.Selection, new Listener() {

public void handleEvent(Event e) {

int hSelection = hBar.getSelection();

int destX = -hSelection - origin.x;

Rectangle rect = image.getBounds();

canvas.scroll(destX, 0, 0, 0, rect.width, rect.height, false);

origin.x = -hSelection;

}

});

final ScrollBar vBar = canvas.getVerticalBar();

vBar.addListener(SWT.Selection, new Listener() {

public void handleEvent(Event e) {

int vSelection = vBar.getSelection();

int destY = -vSelection - origin.y;

Rectangle rect = image.getBounds();

canvas.scroll(0, destY, 0, 0, rect.width, rect.height, false);

origin.y = -vSelection;

}

});

canvas.addListener(SWT.Resize, new Listener() {

public void handleEvent(Event e) {

Rectangle rect = image.getBounds();

Rectangle client = canvas.getClientArea();

hBar.setMaximum(rect.width);

vBar.setMaximum(rect.height);

hBar.setThumb(Math.min(rect.width, client.width));

vBar.setThumb(Math.min(rect.height, client.height));

int hPage = rect.width - client.width;

int vPage = rect.height - client.height;

int hSelection = hBar.getSelection();

int vSelection = vBar.getSelection();

if (hSelection >= hPage) {

if (hPage <= 0)

hSelection = 0;

origin.x = -hSelection;

}

if (vSelection >= vPage) {

if (vPage <= 0)

vSelection = 0;

origin.y = -vSelection;

}

canvas.redraw();

}

});

canvas.addListener(SWT.Paint, new Listener() {

public void handleEvent(Event e) {

GC gc = e.gc;

gc.drawImage(image, origin.x, origin.y);

Rectangle rect = image.getBounds();

Rectangle client = canvas.getClientArea();

int marginWidth = client.width - rect.width;

if (marginWidth > 0) {

gc.fillRectangle(rect.width, 0, marginWidth, client.height);

}

int marginHeight = client.height - rect.height;

if (marginHeight > 0) {

gc

.fillRectangle(0, rect.height, client.width,marginHeight);

}

}

});

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值