I have a project where I am reading an image from an sql server. The image is stored as a varbinary(max). When I get the image from the table and insert it into a folder the image becomes distorted accessing it from the folder. Is there a good method of getting the image without it being distored, or is there a method or way to fix the distortions or blurry image?
Loading
Amit MohantyPosted Oct 19, 2023, 6:36 AM
To enlarge images without distortion or blurriness, you should use interpolation techniques. Interpolation helps in resizing the image smoothly, preserving as much detail as possible. Try this:
James GlissonPosted Oct 19, 2023, 6:06 AM
The probbem with the Code is that it only creates a very small image. If the image gets bigger it becomes distored. I need to enlarge the image without it being distored or blurry.
public class CCurePersonnel
{
public Int64 employee_num { get; set; }
public Image ImgPicture { get; set; }
public byte[] bytPicture { get; set; }
public int targetWidth = 500;
public int targetHeight = 600;
}
static void Main(string[] args)
{
CCurePersonnel[] personnel = new CCurePersonnel[100000];
writeCcureImageToFile(ref personnel);
} // End Main
public static void writeCcureImageToFile(ref CCurePersonnel[] personnel)
{
// Create get File Path for the images
string strPath = @"\\mustang\raptor\c-cure\c-cure\9000Images\";
if (Directory.Exists(strPath))
{
Console.WriteLine(strPath + " File Exist");
}
else
{
Console.WriteLine("File Does Not exist");
Environment.Exit(0);
}
// Insert the txt files
string strConn = ConfigurationManager.ConnectionStrings["CCURE_CONN"].ConnectionString;
SqlConnection conn = new SqlConnection(strConn);
string strQuery = @"SELECT [Int2] as Employee_Num ,[PrimaryPortrait] FROM [ACVSCore].[Access].[Personnel] Where int2 > 0 and PrimaryPortrait is not null and Int2 = '36160'";
SqlCommand cmd = new SqlCommand(strQuery, conn);
conn.Open();
try
{
SqlDataReader reader = cmd.ExecuteReader();
if (reader.HasRows)
{
int i = 0;
while (reader.Read())
{
personnel[i] = new CCurePersonnel();
personnel[i].employee_num = reader.GetInt64(0);
string strEmployeeNum = reader.GetValue(1).ToString();
personnel[i].bytPicture = (byte[])reader.GetSqlBinary(1);
Console.WriteLine("Read Size = {0} Image Size {1}", personnel[i].employee_num, personnel[i].bytPicture.Length);
if ((strEmployeeNum != null) && (personnel[i].bytPicture[i] != null))
{
// Create the file name
string fileName = strPath + personnel[i].employee_num + ".jpg";
using (var ms = new MemoryStream(personnel[i].bytPicture))
{
personnel[i].ImgPicture = Image.FromStream(ms);
Console.WriteLine("Image Picture {0}", personnel[i].ImgPicture);
}
Console.WriteLine("fileName {0}", fileName);
SaveData(ref fileName, personnel[i].bytPicture., personnel[i].targetWidth, personnel[i].targetHeight);
}
i++;
}
reader.Close();
}
cmd.Dispose();
conn.Close();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message.ToString());
}
}// End insertPictures
public static bool SaveData(ref string fileName, byte[] bytPicture, int intWidth, int intHeight)
{
try
{
using (BinaryWriter binWriter = new BinaryWriter(File.Open(fileName, FileMode.Append)))
{
// Write string
binWriter.Write(bytPicture);
}
// img = img.Save(fileName, bytPicture);
}
catch (Exception ex)
{
return false;
}
return true;
}
Amit MohantyPosted Oct 18, 2023, 5:41 AM
Hey James,
Could ypu please provide the code, so we can better understand the problem.