Is there a better way loading a bitmap, rotating it then saving it again? BeginInit ; bi. OnDemand; bi. DelayCreation; bi. Rotate; bi. EndInit ; if File. SavePictureToDisc selectedProject. BeginInit ; newBI. OnLoad; newBI. DelayCreation; newBI. PicturePath, UriKind.
Absolute ; newBI. EndInit ; picItem. Pictures where p. ID select p; if query. Improve this question. Cindy Meister MrCatacroquer MrCatacroquer 1, 3 3 gold badges 15 15 silver badges 21 21 bronze badges.
Add a comment. Active Oldest Votes. Here is my approach to opening an image without locking the file FromStream ms ;. Improve this answer. Brian Brian You still have to keep the stream open for the life of the image, which means the data is in memory twice.
This is impracticable for larger images. Peraps I shouldn't complain however that i. The image uses the stream after it is created sorry, I don't recall the scenarios where it does. If you dispose of it, it will throw an exception. It's not actually important to call Dispose on a MemoryStream since it doesn't use any system resources such as a file lock when using a FileStream. The GC will clean it up properly when it is time.
Thanks, this helped me a lot. It holds native GDI objects that must be destroyed. Show 5 more comments. This is a common locking question widely discussed over the web. Next, the way with per pixel replication doesn't seem to be solid, at least it is noisy.
There's a minor problem with this in that the pixel format will always be bit ARGB and the resolution will always be 96 dpi. This may be OK for most applications, but for some applications it's important to try to maintain the pixel format and resolution of the source file. DrawImage into a Bitmap with matching characteristics seems the simplest way to transfer the Bitmap, without losing pixel format.
To preserve dpi, see my comment on that answer. ToolmakerSteve This does not work for indexed formats, though.
The only way to do it for those is using LockBits to copy of the backing array, and then restore the palette. FYI, also see Brian's answer , which avoids creating bitmap twice, so less overhead; instead it uses a byte array as the intermediate to "break" the dependency on the file. Nyerguds: Thanks, that saved me some time. In combination with the hint from RenniePet, it's a good solution: 1 Load Bitmap 1 from file. Height Buffer. BlockCopy bitmapData1. ToPointer , bitmapData2.
ToPointer , copySize, copySize from first to second bitmap this will require unsafe on the method, but you can work around it using 2x Marshal. ToolmakerSteve 9, 10 10 gold badges 74 74 silver badges bronze badges.
Pondidum Pondidum As posted below by Computer Linguist. BrainSlugs83 your comment is correct, however we can not control what our users do, you should downvote incorrect answer if you feel they are wrong or actively harmful — Sam Saffron. This answers still fails. This doesn't work, it changes the PixelFormat and maybe other things — JochemKempe. As mentioned in BrainSlugs answer , and verified by Anlo , "Clone" does not help the situation. Other answers are safer.
Show 1 more comment. Width, b. Height, b. DrawImage b, Point. This method loads the image file as usual. It then creates a new Bitmap , passing its constructor the original Bitmap as a parameter. That makes a copy of the original. The using statement disposes of the original Bitmap so the file is no longer locked.
Now if you try to rename or delete the file, you will succeed.
0コメント