本地图片查看

package common;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ShowImageServlet extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {

try {
String path=req.getParameter(“path”);
System.out.println(“===============”+path);
FileInputStream hFile = new FileInputStream(path); // 以byte流的方式打开文件
// d:\1.gif
int i = hFile.available(); // 得到文件大小
byte data[] = new byte[i];
hFile.read(data); // 读数据
hFile.close();
res.setContentType(“image/*”); // 设置返回的文件类型
OutputStream toClient = res.getOutputStream(); // 得到向客户端输出二进制数据的对象
toClient.write(data); // 输出数据
toClient.close();
} catch (IOException e) // 错误处理
{
PrintWriter toClient = res.getWriter(); // 得到向客户端输出文本的对象
res.setContentType(“text/html;charset=gb2312”);
toClient.write(“picture is not found!”);
toClient.close();
}

}

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在 WPF 中查看本地图片,你可以使用 Image 控件。首先,你需要将图片文件的路径设置为 Image 控件的 Source 属性。这可以通过在 XAML 中使用绝对或相对路径来完成,例如: ```xaml <Image Source="C:\Images\example.png" /> ``` 或者,如果你的图像文件位于项目文件夹中的 Images 文件夹中,你可以使用相对路径: ```xaml <Image Source="Images\example.png" /> ``` 另外,你还可以通过使用绑定来动态设置图像文件的路径。这可以通过在 ViewModel 中定义一个属性来完成,并将其绑定到 Image 控件的 Source 属性。例如: ```xaml <Image Source="{Binding ImagePath}" /> ``` 在 ViewModel 中,你可以将 ImagePath 属性设置为本地图片文件的路径。这样,当 ImagePath 属性值更改时,Image 控件的 Source 属性也会自动更新。 ```csharp public class MainViewModel : INotifyPropertyChanged { private string _imagePath; public string ImagePath { get { return _imagePath; } set { _imagePath = value; OnPropertyChanged(nameof(ImagePath)); } } public MainViewModel() { // 设置默认的图片路径 ImagePath = @"C:\Images\example.png"; } public event PropertyChangedEventHandler PropertyChanged; protected virtual void OnPropertyChanged(string propertyName) { PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); } } ``` 使用这种方式,你可以动态地更改图像文件的路径,并在 WPF 应用程序中查看本地图片

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值