把cad导出的.xls加载到vb.net的BufferedGraphics里图像是水平相反的,如何水平翻转过来
public AffineTransform flip(ImageIcon icon,int x,int y) {undefined
Image img = icon.getImage();
AffineTransform transform = new AffineTransform();
transform.scale(-1, 1);
transform.translate(-2 * img.getWidth(null) / 2,
0 * img.getHeight(null) / 2);
transform.translate((double) -x, (double) y);
return transform;
}
将图片显示在屏幕上,则用Graphics2D类中的drawImage()方法。
代码:
Graphics2D g2 = (Graphics2D)g;
g2.drawImage(img,transform,this) ;
package com.zeph.j2se.image;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
public class ImageOperate {
/**
* 旋转图片为指定角度
*
* @param bufferedimage
* 目标图像
* @param degree
* 旋转角度
* @return
*/
public static BufferedImage rotateImage(final BufferedImage bufferedimage,
final int degree) {
int w = bufferedimage.getWidth();
int h = bufferedimage.getHeight();
int type = bufferedimage.getColorModel().getTransparency();
BufferedImage img;
Graphics2D graphics2d;
(graphics2d = (img = new BufferedImage(w, h, type)).createGraphics())
.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
RenderingHints.VALUE_INTERPOLATION_BILINEAR);
graphics2d.rotate(Math.toRadians(degree), w / 2, h / 2);
graphics2d.drawImage(bufferedimage, 0, 0, null);
graphics2d.dispose();
return img;
}
/**
* 变更图像为指定大小
*
* @param bufferedimage
* 目标图像
* @param w
* 宽
* @param h
* 高
* @return
*/
public static BufferedImage resizeImage(final BufferedImage bufferedimage,
final int w, final int h) {
int type = bufferedimage.getColorModel().getTransparency();
BufferedImage img;
Graphics2D graphics2d;
(graphics2d = (img = new BufferedImage(w, h, type)).createGraphics())
.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
RenderingHints.VALUE_INTERPOLATION_BILINEAR);
graphics2d.drawImage(bufferedimage, 0, 0, w, h, 0, 0,
bufferedimage.getWidth(), bufferedimage.getHeight(), null);
graphics2d.dispose();
return img;
}
/**
* 水平翻转图像
*
* @param bufferedimage
* 目标图像
* @return
*/
public static BufferedImage flipImage(final BufferedImage bufferedimage) {
int w = bufferedimage.getWidth();
int h = bufferedimage.getHeight();
BufferedImage img;
Graphics2D graphics2d;
(graphics2d = (img = new BufferedImage(w, h, bufferedimage
.getColorModel().getTransparency())).createGraphics())
.drawImage(bufferedimage, 0, 0, w, h, w, 0, 0, h, null);
graphics2d.dispose();
return img;
}
}
.xls是用BufferedGraphics的graphics绘制出来的吗?如果是可以先翻转矩阵,再绘制应该就可以了。
' Form Load 事件, PictureBox 读入图片
Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
PictureBox1.SizeMode = PictureBoxSizeMode.AutoSize ' 设定PictureBox 自动调整大小
PictureBox1.Load("C:\Picture\Genius.jpg") ' PictureBox 载入影像
End Sub
' Button Click 事件, 设定PictureBox 翻转/ 旋转
Private Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
' 宣告建立Bitmap 影像物件
' Bitmap(Image) : 指定影像初始化Bitmap 类别的新执行个体。
Dim bmp As New Bitmap(PictureBox1.Image)
' 指定影像旋转的方向和用来翻转影像的座标轴。
bmp.RotateFlip(RotateFlipType.Rotate180FlipNone) ' 指定不翻转的180 度旋转
' Rotate180FlipNone 请参考底下列举型别设定
PictureBox1.Image = bmp ' 设定PictureBox 显示Bitmap 影像物件的影像。
End Sub
//水平翻转
Bitmap bmp=new Bitmap();
bmp.RotateFlip(RotateFlipType.RotateNoneFlipX);