Home > Net >  C # Image. The Save () efficiency is slow
C # Image. The Save () efficiency is slow

Time:11-23

Doing a bulk copy the photos to the specified path of small programs, by the way, when copying the photos of the vertical flip is positive, then Save is found, the Image. The Save () is very slow, efficiency of impact speed, speed even reach USB2.0, and tried to use threads for multiple an Image. The Save (), efficiency is slow,
Write the test speed, when there is no rotation, directly through the FileInfo. CopyTo way soon, to start speed directly,
What is IO bottleneck or Image. The Save (), the method of blocking?

Private void CopyPhoto (List Files, string directoryTarget, bool isTurn)
{
System. Drawing. Imaging. Encoder myEncoder=System. Drawing. Imaging. Encoder. Quality;
ImageCodecInfo myImageCodecInfo=GetEncoderInfo (" image/jpeg ");
EncoderParameters myEncoderParameters=new EncoderParameters (1);
EncoderParameter myEncoderParameter=new EncoderParameter (myEncoder, 95 l);
MyEncoderParameters. Param [0]=myEncoderParameter;


for (int i=0; I & lt; Files. The Count; I++)
{
//file path
String tpath=System.IO.Path.Com bine (directoryTarget, files [I] FullName. Substring (8));
Try
{
If (isTurn)
{
FileCopyAndDealImg (files [I]. FullName, tpath, myImageCodecInfo, myEncoderParameters);
}
The else
{
Files [I] CopyTo (tpath, true);

}
}
Catch
{
}

}
}


Private void FileCopyAndDealImg (string filepath, string savepath, ImageCodecInfo myImageCodecInfo, EncoderParameters myEncoderParameters)
{
Using (System. Drawing. The Image _thisImage=System. Drawing. The Image. The FromFile (filepath))
{
//processing images, now is blocked or slow
//try
//{
//_thisImage RemovePropertyItem (274);
//}
//catch {}
//try
//{
//_thisImage RemovePropertyItem (20507);
//}
//catch {}
//try
//{
//_thisImage RemovePropertyItem (20521);
//}
//catch {}

_thisImage. RotateFlip (RotateFlipType. Rotate180FlipNone);

_thisImage. Save (savepath, myImageCodecInfo, myEncoderParameters);
_thisImage. The Dispose ();
}
}



Public static ImageCodecInfo GetEncoderInfo (string mimeType)

{
Int j;
ImageCodecInfo [] encoders;
Encoders=ImageCodecInfo. GetImageEncoders ();
For (j=0; J & lt; Encoders. Length; + + j)
{
If (encoders [j] MimeType==MimeType)
Return encoders [j];
}
return null;
}

CodePudding user response:

Concurrent operations research early in the morning, found that the Image this class cannot be concurrent operation, the estimated multithreading optimization to time cannot open

Public void Run1 ()
{
System. Drawing. The Image _serverImage=System. Drawing. The Image. The FromFile (textBox1. Text. The Trim ());
_serverImage. Save (" D: \ \ Photo \ \ Parallel_test1 JPG ", myImageCodecInfo, myEncoderParameters);
_serverImage. The Dispose ();

}
Public void Run2 ()
{
//Thread. Sleep (2000);


System. Drawing. The Image _serverImage=System. Drawing. The Image. The FromFile (textBox3. Text. The Trim ());
_serverImage. Save (" D: \ \ Photo \ \ Parallel_test2 JPG ", myImageCodecInfo, myEncoderParameters);
_serverImage. The Dispose ();
}

Public void ParallelInvokeMethod ()
{
A Stopwatch Stopwatch=new Stopwatch ();
A stopWatch. Start ();
The Parallel. Invoke (Run1 Run2);
A stopWatch. Stop ();
MessageBox. Show (" the Parallel run "+ stopWatch. ElapsedMilliseconds +" Ms. ");

A stopWatch. Restart ();
Run1 ();
Run2 ();
A stopWatch. Stop ();
MessageBox. Show (" Normal run "+ stopWatch. ElapsedMilliseconds +" Ms. ");
}

CodePudding user response:

Found a strange problem, the above method. The Parallel Invoke on the console can normal run concurrently

CodePudding user response:

The _thisImage. RotateFlip (RotateFlipType. Rotate180FlipNone); Annotation will try faster?
And see if it is spinning slow or _thisImage. Save (savepath, myImageCodecInfo, myEncoderParameters); slow

CodePudding user response:

The
reference 3 floor weixin_39604653 response:
the _thisImage. RotateFlip (RotateFlipType. Rotate180FlipNone); Annotation will try faster?
And see if it is spinning slow or _thisImage. Save (savepath, myImageCodecInfo, myEncoderParameters); Slow
tried to comment the same slow, estimation is the problem of the save
  •  Tags:  
  • C#
  • Related