“MediaTypeNames.Application”未包含“DoEvents”的定义
时间: 2023-11-09 14:04:37 浏览: 63
这个错误是因为在你的代码中使用了一个名为“DoEvents”的方法,但是此方法在MediaTypeNames.Application命名空间中没有定义。可能是因为你意外地将其与Windows Forms或WPF应用程序中的DoEvents方法混淆了。
你可以通过以下两种方式解决这个问题:
1. 更改方法名称 - 重命名你的方法以避免与MediaTypeNames.Application命名空间中的方法冲突。这是解决名称冲突的最常见方法。
2. 引用正确的命名空间 - 如果你确实需要使用DoEvents方法,请确保你引用了正确的命名空间。在C#中,你可以使用以下代码从System.Windows.Forms命名空间中调用DoEvents方法:
```csharp
System.Windows.Forms.Application.DoEvents();
```
希望这可以帮助你解决问题。
相关问题
使用picturebox插入图片时显示Image是system.drawing.Image和system.net.Mime.MediaTypeNames.Image
这问题可能是因为您使用了错误的命名空间或引用了不同的库,导致系统无法确定 Image 的类型。
您需要确保在使用 `PictureBox` 控件时,使用的是 `System.Windows.Forms` 命名空间下的 `PictureBox` 类。同时,您需要在代码文件的开头添加以下引用:
```csharp
using System.Drawing;
```
这样您就可以使用 `Image` 类型了,并且不会与 `System.Net.Mime.MediaTypeNames.Image` 混淆。
翻译为java代码 public FileContentResult ShowImg(int id) { var accessory = ProviderManager.GetProvider<AccessoryProviderBase>().FirstOrDefault<Accessory>(t => t.Id == id); var attachment = ProviderManager.GetProvider<AttachmentProviderBase>().FirstOrDefault<Attachment>(t => t.Id == accessory.AttachmentId); var path = Path.Combine(AttachmentProviderBase.FilePath, attachment.RelativePath); byte[] bytes = System.IO.File.ReadAllBytes(path); if (bytes != null) { return File(bytes, MediaTypeNames.Image.Jpeg); } return null; }
以下是 Java 代码的翻译,实现的功能与 C# 代码相同:
```
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.method.annotation.StreamingResponseBody;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
@Controller
public class ImageController {
@GetMapping("/image/{id}")
@ResponseBody
public StreamingResponseBody showImage(@PathVariable("id") int id) throws IOException {
Accessory accessory = ProviderManager.getProvider(AccessoryProviderBase.class).findFirst(Accessory.class, t -> t.getId() == id);
Attachment attachment = ProviderManager.getProvider(AttachmentProviderBase.class).findFirst(Attachment.class, t -> t.getId() == accessory.getAttachmentId());
Path imagePath = Paths.get(AttachmentProviderBase.getFilePath(), attachment.getRelativePath());
byte[] imageBytes = Files.readAllBytes(imagePath);
return outputStream -> {
outputStream.write(imageBytes);
outputStream.flush();
};
}
}
```
需要注意的是,Java 代码中使用了 Spring MVC 框架和 Java 8 的 NIO API 实现了图片的读取和返回。同时,Java 代码中使用了 lambda 表达式作为查询条件,这是 Java 8 中新增的语法特性。