I have a following exception on my report:
I think i tried every possible way to fix it, I even changed the way I load that image to load it from base64 string:
var bytes = Convert.FromBase64String(#"base64string");
using (var ms = new MemoryStream(bytes))
{
pbLogo.Value = Image.FromStream(ms);
}
The problem is that it's non deterministic - sometimes it will work and sometines and will only appear from time to time, without any way to always reproduce it.
Edit - this is a designer code:
// pbLogo
//
this.pbLogo = new Telerik.Reporting.PictureBox()
this.pbLogo.Location = new Telerik.Reporting.Drawing.PointU(Telerik.Reporting.Drawing.Unit.Inch(0D), Telerik.Reporting.Drawing.Unit.Inch(3.9418537198798731E-05D));
this.pbLogo.MimeType = "";
this.pbLogo.Name = "pbLogo";
this.pbLogo.Size = new Telerik.Reporting.Drawing.SizeU(Telerik.Reporting.Drawing.Unit.Pixel(185D), Telerik.Reporting.Drawing.Unit.Pixel(42D));
this.pbLogo.Sizing = Telerik.Reporting.Drawing.ImageSizeMode.ScaleProportional;
this.pbLogo.Style.BackgroundImage.MimeType = "image/jpeg";
this.pbLogo.Style.BackgroundImage.Repeat = Telerik.Reporting.Drawing.BackgroundRepeat.NoRepeat;
this.pbLogo.Value = "";
Related
I'm developing a function to verify if the ISO has Joliet extension.
I use DiscUtils to create the ISO file as follow
CDBuilder builder = new CDBuilder();
builder.UseJoliet = true;
builder.VolumeIdentifier = "A_SAMPLE_DISK";
builder.AddFile("x/x.png", #"C:\Users\Circle\Pictures\Image 1.png");
builder.Build(#"C:\temp\sample.iso");
However, when I read the ISO file. It doesn't be recognized as Joliet
using (FileStream isoStream = File.Open(#"C:\temp\sample.iso", FileMode.Open))
{
CDReader cd = new CDReader(isoStream, true);
if (cd.ActiveVariant == Iso9660Variant.Joliet)
{
// Never enter here
}
}
Not sure which part I did wrong. Any suggestions?
You don't appear to be doing it wrong; however the code won't ever set that ActiveVariant.
If you look at the underlying code, it seems to switch the ActiveVariant to Iso9660Variant.Iso9660 for joliet extensions for the purposes of that field. I don't know the reason for that - it might be a bug, it might have some other esoteric reason for doing it based on some other code in the project.
I've added a couple of comments to the code, and reproduced it here.
case Iso9660Variant.Joliet:
if (svdPos != 0) // <-- Joliet is always a supplementary table.
{
data.Position = svdPos;
data.Read(buffer, 0, IsoUtilities.SectorSize);
SupplementaryVolumeDescriptor volDesc = new SupplementaryVolumeDescriptor(buffer, 0);
Context = new IsoContext { VolumeDescriptor = volDesc, DataStream = _data };
RootDirectory = new ReaderDirectory(Context,
new ReaderDirEntry(Context, volDesc.RootDirectory));
ActiveVariant = Iso9660Variant.Iso9660; // <-- set active variant to base Iso9660
}
break;
I am trying to generate PDF, but I have some problems with the bytes error that hanging the system and web.
I found some reason why but could not find a solution yet.
When I debug, my codec stopped here and freeze my web. Then after I restart it will be very slowly loading my page.
I found out that if I wrote more than 200 characters, it will bug the system. But if I don't, everything will be fine. I just want to know why.
I am not very expert here. Thanks for you help. This is the codec debug where it stopped and frozen. Can anyone help me?
Dim bytes() As Byte
Dim fs As FileStream = New FileStream(strNewPathPDF, FileMode.Open, FileAccess.Read)
Dim reader As BinaryReader = New BinaryReader(fs)
bytes = reader.ReadBytes(CType(fs.Length, Integer))
fs.Close()
You get the nreco dll from here: https://www.nuget.org/packages/NReco.PdfGenerator/
And you can use the following code:
NReco.PdfGenerator.HtmlToPdfConverter nPdf = new NReco.PdfGenerator.HtmlToPdfConverter();
nPdf.Size = PageSize.Letter;
PageMargins mar = new PageMargins();
mar.Bottom = -5;
mar.Left = -5;
mar.Top = -5;
mar.Right = -5;
nPdf.Margins = mar;
nPdf.Orientation = PageOrientation.Landscape;
var pdffilename = "D:\Documents"+"filename" + ".pdf";
var htmltext="this testing pdf"
var pdfBytes = nPdf.GeneratePdf(htmltext);
File.WriteAllBytes(pdffilename, pdfBytes);
I was having trouble editing or removing keywords from a photograph. The following works to replace the keywords successfully:
...
string s_keywords = "tag1;tag2;tag3";
PropertyItem item_keyword = (PropertyItem)FormatterServices.GetUninitializedObject(typeof(PropertyItem));
item_keyword.Id = 0x9c9e; // XPKeywords
item_keyword.Type = 1;
item_keyword.Value = System.Text.Encoding.Unicode.GetBytes(s_keywords + "\0");
item_keyword.Len = item_keyword.Value.Length;
image.SetPropertyItem(item_keyword);
...
Note that my experiments show that image.RemovePropertyItem(0x9c9e); seems to have no effect on the saved image. Instead use the above code with s_keywords = "";
Don't do it this way: The following code works to remove the keywords, but results in the jpeg being re-encoded and loosing some quality (the image file goes from about 4MB to < 2MB and I can see some slight visual differences):
...
Image image_copy = new Bitmap(image);
foreach (var pi in image.PropertyItems)
{
if (pi.Id != 0x9c9e) image_copy.SetPropertyItem(pi);
}
image.Dispose();
image = (Image)image_copy.Clone();
...
I'm was having similar issues with setting the XPTitle - setting the propertyItem 0x9c9b seemed to have no effect in the saved image, instead I had to open the file as a BitmapFrame, extract the BitmapMetadata and use the Title property, then build a new jpeg using JpegBitmapEncoder - thus re-encoding and loosing image quality.
...
BitmapFrame bf_title = BitmapFrame.Create(new Uri(tmp_file, UriKind.Relative));
BitmapMetadata bmd_title = (BitmapMetadata)bf_title.Metadata.Clone();
bmd_title.Title = new_title;
BitmapFrame bf_new = BitmapFrame.Create(bf_title, bf_title.Thumbnail, bmd_title, bf_title.ColorContexts);
JpegBitmapEncoder je = new JpegBitmapEncoder();
je.Frames.Add(bf_new);
FileStream fs = new FileStream(tmp_file, FileMode.Create);
je.Save(fs);
fs.Close();
...
See my answer below for the correct way to change the title.
This was driving me crazy, I hope this can help someone else...
Note that the above keywords code now works perfectly.
I'm answering this because I could not find sample code to do this when I searched - so hopefully someone else will find this useful.
To change the title use the following code. The problem was that there are two tags that affect how windows shows the title - one of which (0x010e) takes priority over the XPTitle (0xc9b) tag...
...
string new_value = "New title for the image";
PropertyItem item_title = (PropertyItem)FormatterServices.GetUninitializedObject(typeof(PropertyItem));
item_title.Id = 0x9c9b; // XPTitle 0x9c9b
item_title.Type = 1;
item_title.Value = System.Text.Encoding.Unicode.GetBytes(new_value + "\0");
item_title.Len = item_title.Value.Length;
image.SetPropertyItem(item_title);
PropertyItem item_title2 = (PropertyItem)FormatterServices.GetUninitializedObject(typeof(PropertyItem));
item_title2.Id = 0x010e; // ImageDescription 0x010e
item_title2.Type = 2;
item_title2.Value = System.Text.Encoding.UTF8.GetBytes(new_value + "\0");
item_title2.Len = item_title2.Value.Length;
image.SetPropertyItem(item_title2);
image.Save("new_filename.jpg", ImageFormat.Jpeg)
image.Dispose();
...
Note - Another potential issue, you need to save the image to a new location. You can then dispose of the image and copy it into the original location if desired.
I need to get DCT-coefficients array after quantization for further changing bits (steganography).
My question is: Lets say, i have jpeg image in picturebox or whatever. How can i acess to dct coef. of this image using C# and library like LibJpeg.Net? Need a code pls. Can't find anything complete and simple on whole web. Also, can't see any tutorial on LibJpeg.Net.
After this steps:
BitMiracle.LibJpeg.Classic.jpeg_decompress_struct oJpegDecompress = new BitMiracle.LibJpeg.Classic.jpeg_decompress_struct();
System.IO.FileStream oFileStreamImage = new System.IO.FileStream(strImagePath, System.IO.FileMode.Open, System.IO.FileAccess.Read);
oJpegDecompress.jpeg_stdio_src(oFileStreamImage);
oJpegDecompress.jpeg_read_header(true);
BitMiracle.LibJpeg.Classic.jvirt_array<BitMiracle.LibJpeg.Classic.JBLOCK>[] JBlock = oJpegDecompress.jpeg_read_coefficients();
what should i do now, to edit dct coeff? Use .Access()? How do i use this? Any examples?
Following:
short[] block = JBlock[c].Access(x, y);
gives an error like that: "Cannot implicitly convert type 'BitMiracle.LibJpeg.Classic.JBLOCK[][]' to 'short[]'"
Also, while using something similar, it gives an error about converting "BitMiracle.LibJpeg.Classic.JBLOCK[][]" to type "System.IConvertible".
Or maybe someone knows another easy way for my problem?
All right, i figured out something. At least, its answering my main question.
private void button1_Click(object sender, EventArgs e)
{
string path = #"D:\067.jpg";
var img = new Bitmap(path);
var jo = img.Width;
var joj = img.Height;
BitMiracle.LibJpeg.Classic.jpeg_decompress_struct oJpegDecompress = new BitMiracle.LibJpeg.Classic.jpeg_decompress_struct();
System.IO.FileStream oFileStreamImage = new System.IO.FileStream(path, System.IO.FileMode.Open, System.IO.FileAccess.Read);
oJpegDecompress.jpeg_stdio_src(oFileStreamImage);
oJpegDecompress.jpeg_read_header(true);
BitMiracle.LibJpeg.Classic.jvirt_array<BitMiracle.LibJpeg.Classic.JBLOCK>[] JBlock = oJpegDecompress.jpeg_read_coefficients();
var ll = JBlock[0].Access(0, 1); // accessing the element
var oo = 5; // its gonna be new value for coefficient
for (int i = 0; i < 64; i++) // some cycle
{
ll[0][i][0] = Convert.ToInt16(oo); // changes
}
oJpegDecompress.jpeg_finish_decompress();
oFileStreamImage.Close();
/////
System.IO.FileStream objFileStreamMegaMap = System.IO.File.Create(#"D:\068.jpg");
BitMiracle.LibJpeg.Classic.jpeg_compress_struct oJpegCompress = new BitMiracle.LibJpeg.Classic.jpeg_compress_struct();
oJpegCompress.jpeg_stdio_dest(objFileStreamMegaMap);
oJpegDecompress.jpeg_copy_critical_parameters(oJpegCompress);
oJpegCompress.Image_height = joj;
oJpegCompress.Image_width = jo;
oJpegCompress.jpeg_write_coefficients(JBlock);
oJpegCompress.jpeg_finish_compress();
objFileStreamMegaMap.Close();
oJpegDecompress.jpeg_abort_decompress();
oFileStreamImage.Close();
}
Little bit sloppy, but still, just the test...
Used some code from here
Like that, as you can see in your console, every 0th element under 0->m_buffer->0->i in the output image will be equal to 5
All hail to me.
I've done this while writing a JPEG library to verify its correctness. You just have to get the source code to LIBJPEG; identify where it does the functions you are interested int (somewhat difficult because the code is convoluted); set a breakpoint or return there.
I have recently started using C# over the past year so I'm somewhat new to this, but can usually hack through things with some effort, but this one is eluding me. We use TestTrack for development bug/issue tracking at our company. I've created a custom windows forms app to be the front-end to TestTrack for one of our departments. It connects using SOAP. I'm not using WPF/WCF and don't want to go that route. I'm having difficulty finding any examples of how to correctly encode a file for attachment that is a PDF. The code below does actually create an attachment in TestTrack to an already-existing issue, but when you try to open it in TestTrack, it pops up an error message that says "Insufficient Data For An Image". The example below does work if you're wanting to add a text file to TestTrack using SOAP. I'm wanting to know what I need to change below so that I can get a PDF file into TestTrack and then be able to open it in the TestTrack application without the error mentioned above. Thanks in advance for any input/help.
public void getAttachments(long lSession, CDefect def)
{
ttsoapcgi cgiengine = new ttsoapcgi();
// Lock the defect for edit.
CDefect lockedDefect = cgiengine.editDefect(lSession, def.recordid, "", false);
string attachment = "c:\\TEST\\TEST_PDF.PDF";
CFileAttachment file = new CFileAttachment();
file.mstrFileName = Path.GetFileName(attachment);
System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding();
StreamReader reader = new StreamReader(attachment);
file.mstrFileName = Path.GetFileName(attachment);
file.mpFileData = enc.GetBytes(reader.ReadToEnd());
reader.Close();
CReportedByRecord reprec = lockedDefect.reportedbylist[0];
CFileAttachment[] afile = reprec.attachmentlist;
if (afile == null)
{
lockedDefect.reportedbylist[0].attachmentlist = new CFileAttachment[1];
lockedDefect.reportedbylist[0].attachmentlist[0] = file;
}
// Save our changes.
cgiengine.saveDefect(lSession, lockedDefect);
}
}
Here is the modified method that allowed me to attach a PDF to SOAP and get it into TestTrack as an attachment to an issue:
public void getAttachments(long lSession, CDefect def)
{
ttsoapcgi cgiengine = new ttsoapcgi();
// Lock the defect for edit.
CDefect lockedDefect = cgiengine.editDefect(lSession, def.recordid, "", false);
string attachment = "c:\\TEST\\TEST_PDF.PDF";
CFileAttachment file = new CFileAttachment();
file.mpFileData = File.ReadAllBytes(attachment);
file.mstrFileName = Path.GetFileName(attachment);
CReportedByRecord reprec = lockedDefect.reportedbylist[0];
CFileAttachment[] afile = reprec.attachmentlist;
if (afile == null)
{
lockedDefect.reportedbylist[0].attachmentlist = new CFileAttachment[1];
lockedDefect.reportedbylist[0].attachmentlist[0] = file;
}
// Save our changes.
cgiengine.saveDefect(lSession, lockedDefect);
}