C# - XFL content: Convert .dat file to bitmap [duplicate] - c#

This question already has answers here:
XFL - What are the ./bin/*.dat files?
(2 answers)
Closed 7 years ago.
I have .fla files that use XFL format.
inside there's a /bin folder with some .dat files, these files are images.
is there a way to convert these .dat file to bitmap and display them in PictureBox control?
here's an example of a dat file: link
And the corresponding image (exported from Flash) link
UPDATE:
below my code:
string scenePath = "Path to .fla file";
ZipFile zip = new ZipFile(scenePath);
MemoryStream ms = new MemoryStream();
foreach (ZipEntry entry in zip)
{
if (entry.FileName.Contains(objName))
{
entry.Extract(ms);
//TODO: Need to convert the content of MemoryStream to image type!
Bitmap bmp = new Bitmap(ms);
pictureBoxObjView.Image = bmp;
}
}
UPDATE2:
I found a post that describe a similar issue XFL - What are the ./bin/*.dat files?.
in the answer I found this :
where the decompressed data are pixels with storage type: ARGB, so with the size info it should be enough to get the image from it. It's using ZLIB compression (www.zlib.net) Flash is using compression level 1, but it's possible to use any level (but it's not necessary as the sources are normally compressed altogether.
but I still don't undestand how to convert the .dat file to bitmap !!
I tried manualy to uncompress the .fla and rename the .dat file to image ext (.jpg, .png, .bmp) to check if it's a normal image file, but I got the error "Incorrect format" when I try to open it.
My problem is how to convert the content of my MemoryStream to Bitmap?
Regards,

If you can extract the files from the /bin folder into a stream or byte array (which would then be encapsulated in a stream) you could call the corresponding Bitmap constructor and simply assign this bitmap to the image property of the PictureBox.

Related

How can I get image content as pixel and create image from it in ASP.NET Core 6 Web API?

I try to save my images on my server, but I can't let my server save file and virus because of that I want to get image content as pixels of rgb and after that I create image by myself.
I can't use bitmap (or other type in C# like bitmapImage, ... etc) and I don't know how I can do this with sixlabors.ImageSharp.
I have some code that I tried but I can't implement the exact logic that I want (code shown here):
[HttpPost("[action]")]
public async Task<IActionResult> Get([FromForm] ImageFormat file)
{
await using var memoryStream = new MemoryStream();
await file.File.CopyToAsync(memoryStream);
IImageFormat format;
using (var image = Image.Load(memoryStream.ToArray(), out format))
{
using (var output = new MemoryStream())
{
image.Save(output, format);
var responseType = format.Name.ToLower();
return File(output.ToArray(), "application/octet-stream", file.File.FileName);
}
}
return null;
}
Can anybody help me with this problem?
i don't see a reason to convert image into image: there are several format zip-algorythms etc.wich you have to support in that case. example jpg is not bitmap, there is convertion issue - quality of image becomes less each conversion time. Image itself is not executable - it can be used only as container for virus body, can't harm your OSystem itself, another executable part should works somewhere.
But even if you would like to store images on disk, in other format - you can convert image to base64 text (one line of code, like example) - it less harmful and well known way to work with any file type. you can zip image by cszip, you can change file name and extension to hide file type.
I don't see a reasson to convert one image to another for this scenario/task.

How do I create/edit a PNG file in C#?

I've written a program that can create digital art. Images like the Mandelbrot Set and the Julia Set. But I'm looking to save these images as PNGs. At present, in Java, I'm generating the images in an application window and then taking a screen shot of the display. However, I lose the finer detail of these images. Plus, this method also reduces the physical size of the images as well. I want to potentially be able to make a big poster out of these pictures.
In C#, I'm using the following:
Bitmap myimage = new Bitmap("image.png"); and: myimage.SetPixel(x,y, Color.FromArgb(255*colors[x,y], 255*colors[x,y], 255*colors[x,y]); where colors[,] is some value between 0 and 1.
The code runs fine, minus the Bitmap declaration. My understanding is that new Bitmap(filepath); allows you to edit and manipulate the PNG image. Am I right to think that? How do I create/edit a PNG file in C#?
(edit)PS: The PNG file, "image.png", does exist in the solution folder.
Firstly you have to know the step by step process in creating the PNG file.
Setup Aspose.Imaging for .NET package from Nuget.org.
Include reference to following two namespaces: Aspose.Imaging,
Aspose.Imaging.ImageOptions.
Specify license using SetLicense method before converting.
Read BMP file into an Image object.
Set attributes for output PNG image using PngOptions class.
Save the output PNG image with the specified PNG options.
Code to create PNG image from BMP
using System;
//Use following namespaces to create PNG image
using Aspose.Imaging;
using Aspose.Imaging.ImageOptions;
namespace CreatePNGImage
{
class Program
{
static void Main(string[] args)
{
//Set license before creating PNG image from BMP
Aspose.Imaging.License AsposeImagingLicense = new Aspose.Imaging.License();
AsposeImagingLicense.SetLicense(#"c:\asposelicense\license.lic");
//load input BMP image
Image BmpToPngImage = Image.Load("InputBMPImage.bmp");
//set attributes of the output PNG file
PngOptions PNGImageOptions = new PngOptions();
PNGImageOptions.ResolutionSettings = new ResolutionSetting(300, 300);
PNGImageOptions.CompressionLevel = 6;
//save converted output PNG image
BmpToPngImage.Save("OutputPNGImage.png", PNGImageOptions);
}
}
}
Try this to create the PNG file out of c#.

C# api octet-stream response body to zip file

I have a service which executes a request to a client api that returns a octet-stream response body with the Content-Disposition header in it (This api is meant to return a zip file.). I am using RestSharp and the DownloadData function to get the response as a byte array, but I want to then save the zip file to my local server.
I have tried using DotNetZip and a MemoryStream to create the zip file by using the following example:
using (MemoryStream stream = new MemoryStream(fileBytes))
{
using (ZipFile zip = new ZipFile())
{
zip.AddEntry("test", stream);
zip.Save(filePath);
}
}
The code above creates a zip file and an entry called test but I cannot open it.
Just to clarify, the zip file I am trying to create contains the following files and folders:
296927a0-5ac7-4ccd-9928-bd74ef7ae68a_20200227074457 (Root folder)
images (folder)
jpg image
jpg image
jpg image
details.json (file)
achievements.json (file)
evaluation.json (file)
Is there any way to achieve this?
As I understood the fileBytes is already a zip file byte stream, what means you don't need to zip it again. Just save as file.
File.WriteAllBytes(filePath, fileBytes);
For anyone else who encounters this problem, as in saving the byte array to disk and getting a corrupted zip file, I have figured out the answer with the help of Dennis Tretyakov.
Basically, when downloading a octet-stream, the first section of is dedicated to the structure of the zip file. In my case the first section of the zip file was:
--c7c2ad44-881c-47c4-89e5-323f91b75269
Content-Disposition: form-data; name="deb08d79-372d-4de2-a684-1298f25fecd9_20200310142909.zip"
Content-Type: application/octet-stream
And thereafter the actual data for the zip file appears after a linebreak.
What I ended up doing was calculating the byte count of the "header" section by converting the byte array to a string:
string stringEncodedBytes = Encoding.ASCII.GetString(response.RawBytes);
Then with knowing the starting point of the zip file data, finding the index of the starting point:
int headerSectionIndex = stringEncodedBytes.IndexOf("PK");
Once I found the index, which in my case always seems to be 178, I simple removed that part of the byte array and then copy the trimmed byte array to a new byte array and write it to disk:
byte[] trimmedFileBytes = new byte[response.RawBytes.Length - headerSectionIndex];
Array.Copy(response.RawBytes, headerSectionIndex, trimmedFileBytes, 0, trimmedFileBytes.Length);
File.WriteAllBytes(filePath, trimmedFileBytes);
I would suggest that anyone struggling with the same issue, open the "corrupted" zip file in notepad++ and take a look at the first section and then determine where the data section of the zip file starts.
The initial zip file data looks like this:
And the trimmed zip file data looks like this:
So i had the same issue Dev_101 had, in which a RestResponse was corrupting the .zip file, however in my case, it was just appending an extra " on the beginning and end, so I just needed to remove the " and Base64Decode it and saved perfectly.

convert a png file to a pcx file using c#

I'm trying to convert a .png file to a .pcx file. The scenario is the following:
I'm using a TSC TTP-343C label printer. On the labels I have to print images. TSC provides a library documentation for developers. Since I can only print images on those labels using pcx files I have to convert all the images to pcx images. Any other format or even incorrect pcx format (e.g. if the user just renamed the file ending) will not be printed on the label.
I've seen this post linking to the Magick library. In this post, the OP is trying to convert a bmp file to a pcx file which is not exactly what I try to achieve. I looked at the Magick documentation about converting images. I tried to convert the images like:
using (MagickImage img = new MagickImage(png)) // png is a string containing the path of the .png file
{
img.Format = MagickFormat.Pcx;
img.Write(pcx); // pcx is a string containing the path of the new .pcx file
}
Unfortunately this is not saving the image correctly. The label printer still cannot print the image on the label. I tried printing a correct pcx file and this worked fine. So I guess the only reason why it's still not working is that the converted file is not a real pcx file.
Is there a way to do such a conversion? If yes, how can I achieve that? My application is a windows forms application, written in C# using .NET framework 4.5.2.
EDIT:
Here you can see an example how to print a label with a pcx file:
TSC.openport(sPrinterName);
TSC.setup("100", "100", "4", "8", "1", "3.42", "0");
TSC.clearbuffer();
TSC.downloadpcx(#"\\PathToPcxFile\test.pcx", "test.pcx");
TSC.sendcommand("PUTPCX 35," + y + ",\"test.pcx\"");
TSC.printlabel("1", "1");
TSC.closeport();
This code works fine on real pcx files. The methods of the TSC library can you find here.
downloadpcx(a,b)
Description: Download mono PCX graphic files to the printer Parameter:
a: string; file name (including file retrieval
path)
b: string, names of files that are to be downloaded in the
printer memory (Please use capital letters)
Source: http://www.tscprinters.com/cms/upload/download_en/DLL_instruction.pdf
EDIT II:
A pcx file which is working (created using photoshop) looks like this (if it helps you):
PCX files are (at best) palette-based.
So to create a valid pcx output you need to add this one line:
using (MagickImage image = new MagickImage(sourcePng))
{
image.Format = MagickFormat.Pcx;
image.ColorType = ColorType.Palette; // <----
image.Write(targetPcx);
}
Your image as pcx file

ByteArray to asp Image [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to show a image in database in the image control of Asp.net?
I am returning a list of products from a database. Inside this list i am saving database images as bytearrays. Currently I am displaying about 30 products on a page and i would like to add their images next to the product information. What would be the easiest way to go about this?
What I have:
public Image PopulatePicture()
{
Image newImage;
//Read image data into a memory stream
using (MemoryStream ms = new MemoryStream(ImageByteArray, 0, ImageByteArray.Length))
{
ms.Write(ImageByteArray, 0, ImageByteArray.Length);
//Set image variable value using memory stream.
newImage = Image.FromStream(ms, true);
}
return newImage;
}
I have an error on Image.FromStream (System.Web.UI.WebControls does not contain a definition for FromStream)
If your using Mvc its rather simple
Simply write an action such as the following
public FileContentResult Image()
{
//Get the Byte Array for your image
byte[] image = FilesBLL.GetImage();
//Return a jpg
//Note the second parameter is the files mime type
return File(image, "image/jpeg");
}
See This Link For Mime types
http://www.webmaster-toolkit.com/mime-types.shtml
I highly suggest adding a column to your files table to store mime types (determine this on upload)
And in your view put down an image like this
<img src='#Url.Action("GalleryMediumThumb")'/>
for webforms see
How to show a image in database in the image control of Asp.net?
You likely have a few issues here:
You are referencing an asp.net Image object (System.Web.UI.WebControls.Image) not the GDI+ System.Drawing.Image object that you want. Clarify your namespace.
You can't dispose the stream on the image. Drop the using block. https://stackoverflow.com/a/13696705/64262
You will need to write the resulting stream to the response stream (negating the need to convert it to an image in the first place), and then reference that endpoint from an <img> tag.
A generic ASP.NET Handler (*.ashx) could load the image from the database and stream it as a normal image to the browser.
May this http://coffeedrivendevelopment.net/2012/10/26/sharing-image-resources-between-wpf-and-asp-net/ will help you. It is about images in ressources, but the way is the same.

Categories

Resources