Comments (1)
I think the library may not handle concurrency correctly. I also get this error in a parallel foreach.
// See https://aka.ms/new-console-template for more information
using Libwebp.Net;
using Libwebp.Standard;
if (args.Length <= 0 || string.IsNullOrWhiteSpace(args[0]))
{
Console.WriteLine("Please provide a file path.");
return;
}
var path = args[0];
ICollection<string> files = new string[] {};
string rootPath = Environment.CurrentDirectory;
if(Directory.Exists(path))
{
files = Directory.GetFiles(path, "*.png");
if(!path.EndsWith('\\'))
{
path += '\\';
}
var directoryName = Path.GetDirectoryName(path);
if(string.IsNullOrWhiteSpace(directoryName))
{
Console.WriteLine("Unable to determine directory name");
return;
}
rootPath = Path.Combine(rootPath, $"{directoryName} - WebP");
if(!Directory.Exists(rootPath))
{
Directory.CreateDirectory(rootPath);
}
}
else if(File.Exists(path) && Path.GetExtension(path) == ".png")
{
files = new[] { path };
}
await Parallel.ForEachAsync(files, async (currentFilePath, token) =>
{
using var currentFile = new FileStream(currentFilePath, FileMode.Open);
using var ms = new MemoryStream((int)currentFile.Length);
await currentFile.CopyToAsync(ms);
var fileName = $"{Path.GetFileNameWithoutExtension(currentFilePath)}.webp";
var outputFileName = Path.Combine(rootPath, fileName);
var encoderConfig = new WebpConfigurationBuilder()
.Lossless()
.Output(fileName)
.Build();
var encoder = new WebpEncoder(encoderConfig);
var tempFile = await encoder.EncodeAsync(ms, fileName);
using var newFile = new FileStream(outputFileName, FileMode.OpenOrCreate);
await tempFile.CopyToAsync(newFile);
await newFile.FlushAsync();
newFile.Close();
});
from libwebp.net.
Related Issues (6)
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from libwebp.net.