嗨,大家我想将所有图像转换为我的目录中的灰度,但是我知道的代码只能一个接一个地完成这个任务!!但是我有很多图像,而且我希望如果有该转换所有图像目录代码为灰度或使用C#将图像转换为灰度使用C#
-1
A
回答
1
假设你有一个名为path
变量,它是处理图像的文件夹的路径,并假设您可以使用WPF的API,将图像转换:
DirectoryInfo dirInfo = new DirectoryInfo(path);
foreach (FileInfo fileInfo in dirInfo.EnumerateFile()) {
ProcessImage(FileInfo.FullName);
}
void ProcessImage(string image) {
byte[] imageData = null;
try {
// Load the data in the file into a byte array for processing.
imageData = File.ReadAllBytes(image);
} catch (Exception) {
// Your error handling code here
}
// We're going to put the image into this object
BitmapImage src = new BitmapImage();
try {
// Load the image into a memory stream, then into src.
using(MemoryStream stream = new MemoryStream(imageData)) {
src.BeginInit();
src.CacheOption = BitmapCacheOption.OnLoad; // Causes the bitmap data to be loaded now, not at a later time.
src.StreamSource = stream;
src.EndInit();
}
} catch (NotSupportedException) {
// The bitmap format is not supported. Your error handler here.
}
try {
// Create a FormatConvertedBitmap object & set it up.
FormatConvertedBitmap dst = new FormatConvertedBitmap();
dst.BeginInit();
dst.DestinationFormat = PixelFormats.Gray8; // Use whatever gray scale format you need here
dst.Source = src;
// Now convert the image to 8 bits per pixel grey scale.
dst.EndInit();
// Compute the dst Bitmap's stride (the length of one row of pixels in bytes).
int stride = ((dst.PixelWidth * dst.Format.BitsPerPixel + 31)/32) * 4;
// Allocate space for the imageBytes array.
imageBytes = new byte[ stride * dst.PixelHeight ];
// Get the pixels out of the dst image and put them into imageBytes.
dst.CopyPixels(Int32Rect.Empty, imageBytes, stride, 0);
} catch (Exception ex) {
// Your error handler here.
}
// Any other code to save or do something else with the image.
}
相关问题
- 1. 使用matplotlib将灰度图像转换为RGB热图像
- 2. 使用Aforge.Net将灰度图像转换为黑白图像
- 3. 将图像转换为灰度图像
- 4. 使用C#将图像转换为SVG
- 5. C#将2D双精度数组转换为灰度图像
- 6. 使用OpenCV iOS将图像转换为黑白(非灰度)Objective C
- 7. 将RGB图像转换为灰度和灰度转换为RGB图像?
- 8. 灰度图像转换Turbo C++
- 9. 使用Aforge.net将Ycbcr图像转换为灰度
- 10. 使用imagemagick将JPG图像从灰度转换为RGB
- 11. Java - 使用文件名数组将图像转换为灰度
- 12. 使用数组矩阵将图像转换为灰度RGB java
- 13. C#将灰度数据转换为彩色图像
- 14. 将简单图像转换为灰度
- 15. 将图像转换为灰度
- 16. 将彩色图像转换为灰度
- 17. 将灰度图像转换为彩色
- 18. jQuery将图像转换为灰度?
- 19. openCV将Mat转换为灰度图像
- 20. 将灰度图像转换为黑白
- 21. 将图像转换为灰度指针
- 22. 灰度图像翻转c或C++?
- 23. 在vC++中使用opencv将rgb图像转换为灰度图像
- 24. 如何在C#中将32bpp图像转换为16 bpp图像? (灰度)
- 25. 如何使用C++将图像文件转换为ASCII图像?
- 26. 使用c将字节转换为图像&图像到字节#
- 27. 八度图像包无法将图像转换为灰度
- 28. 在C中使用emguCV转换为灰度模式#
- 29. 图像转换为灰度使用ImageMagick很暗
- 30. 将C数组中存储的像素值反转为灰度图像C
[C#,可将图像转换为灰度](http://stackoverflow.com/questions/2265910/c-convert-image-to-grayscale) – Annabelle
Th是一个问题和答案网站来帮助解决发展问题。你可以用google搜索这个问题,并找到很多答案。考虑向我们展示您尝试过的一些代码,我们很乐意为您提供帮助。 –
搜索“批量转换图像”或“批量转换图像”。有许多不同的程序会为你做到这一点。 –